From: Mr_John on
i have already built an iterative web server that serves just one
request at time.im trying to build now a concurrent server that can
serve multiple request at time(using posix library).exactly im using a
pool of thread pre-allocated.each thread waits till are coming
connections, manages his own request and serves the browser.using the
following code am i on the right way?
thanks

#include <pthread.h>
....
#define NUM_THREADS 10


int num[NUM_THREADS],i;


int main (void){

pthread_t threads[NUM_THREADS];
pthread_mutex m=PTHREADS_MUTEX_INITIALIZER;

/*building a pool of threads*/
for(i=0;i<NUM_THREADS;i++){
num[i]=i;
pthread_create(&threads[i],NULL,manage,&num[i]);
}
....
/* listen() */

while(1){
for(i=0;i<NUM_THREADS;i++)
pthread_join(threads[i],NULL);
}

}

void* manage(void*arg){
int id;
id=*((int *)arg);
while(1){

pthread_mutex_lock(&m);

printf("[Thread # %i] Waiting request\n",id)
/*accept()*/
/*receive message, manage the request and send to
browser*/
/*close(cl_sk);*/

pthread_mutex_unlock(&m);

}
}
From: David Schwartz on
On Jun 25, 3:57 am, Mr_John <lecter.hanni...(a)email.it> wrote:

> void* manage(void*arg){
>       int id;
>       id=*((int *)arg);
>       while(1){
>
>                pthread_mutex_lock(&m);
>
>                printf("[Thread # %i] Waiting request\n",id)
>                /*accept()*/
>                /*receive message, manage the request and send to
> browser*/
>                /*close(cl_sk);*/
>
>                pthread_mutex_unlock(&m);
>
>       }
>
> }

If you hold a lock while you do everything, that will only allow one
thread to run at a time. You should only hold a lock while you are
accessing a shared resource.

DS
From: Mr_John on
On 25 Giu, 14:39, David Schwartz <dav...(a)webmaster.com> wrote:
> On Jun 25, 3:57 am, Mr_John <lecter.hanni...(a)email.it> wrote:
>

> > void* manage(void*arg){
> > int id;
> > id=*((int *)arg);
> > while(1){
>
printf("[Thread # %i] Waiting request\n",id)
> >
pthread_mutex_lock(&m);
/*accept()*/
pthread_mutex_unlock(&m);

> > /*receive message, manage the request and send to
> > browser*/
> > /*close(cl_sk);*/
>
> >
>
> > }
>
> > }
>
> If you hold a lock while you do everything, that will only allow one
> thread to run at a time. You should only hold a lock while you are
> accessing a shared resource.

so if i lock just the part when make the accept() (that use the shared
socket created in the main) the rest of the body can be executed while
other threads are making their accept().see if the code modified is
correct.thanks a lot

From: Raxit on
On Jun 25, 7:04 pm, Mr_John <lecter.hanni...(a)email.it> wrote:
> On 25 Giu, 14:39, David Schwartz <dav...(a)webmaster.com> wrote:
>
> > On Jun 25, 3:57 am, Mr_John <lecter.hanni...(a)email.it> wrote:
>
> > > void* manage(void*arg){
> > >       int id;
> > >       id=*((int *)arg);
> > >       while(1){
>
>                      printf("[Thread # %i] Waiting request\n",id)
>
>                      pthread_mutex_lock(&m);
>                      /*accept()*/
>                      pthread_mutex_unlock(&m);
>
> > >                /*receive message, manage the request and send to
> > >                browser*/
> > >                /*close(cl_sk);*/
>
> > >       }
>
> > > }
>
> > If you hold a lock while you do everything, that will only allow one
> > thread to run at a time. You should only hold a lock while you are
> > accessing a shared resource.
>
> so if i lock just the part when make the accept() (that use the shared
> socket created in the main) the rest of the body can be executed while
> other threads are making their accept().see if the code modified is
> correct.thanks a lot

I think in all modern system, you no need to lock even accept !
i think above code is only prototype. seems fine.

-Raxit
From: David Schwartz on
On Jun 25, 7:20 am, "Ra...(a)MyKavita.com" <raxitsheth2...(a)gmail.com>
wrote:

> I think in all modern system, you no need to lock even accept !
> i think above code is only prototype. seems fine.
>
> -Raxit

Right. It's been a long time since concurrent 'accept's were a
problem.

DS