3

I have written a C/C++ code which implements socket connection and the main thread is in continuous loop listening at its port. When a connection request comes at this port, I have spawned a thread using pthread calls and offloaded the work on this thread. As such i have 'n' threads getting created dynamically for 'n' incoming requests. The problem is that, if one thread terminates the main thread also terminates.

I have used pthread_join() but It waits for the thread in the argument to finish.In my case, the new threads are not getting spawned once the call to pthread_join() is made.

pthread_t t;
while(1)    //server always to be in listen mode
 {
  client_len=sizeof(client_sockaddr);
  client_sockfd=accept(server_sockfd,(struct sockaddr*)&client_sockaddr,&client_len);


  pthread_create(&t,NULL,server_thread,(void*)client_sockfd);

  (void)pthread_join(t,NULL);

  }
Yogesh lele
  • 392
  • 4
  • 17
  • 1
    Can you post a full example that shows the main thread exiting when a child thread exits please? – simonc Jul 23 '13 at 15:59
  • detach the pthread so that when it finishes it goes back to the system. – CBIII Jul 23 '13 at 16:00
  • You're going to need an array of pthread_t's not just 1... if you're trying to create multiple threads. – Scotty Bauer Jul 23 '13 at 16:00
  • If he is trying to use pooling he will need an array. – CBIII Jul 23 '13 at 16:01
  • You're right @ClydeByrdIII. Assuming he didn't want any return values, which it doesn't look like he does detachment would work, and you wouldn't need the array. – Scotty Bauer Jul 23 '13 at 16:03
  • @ClydeByrdIII If i detach the pthread, will it stay alive even after the main completes the execution? – Yogesh lele Jul 23 '13 at 16:09
  • @ScottyBauer Also tried with array but the same problem. – Yogesh lele Jul 23 '13 at 16:09
  • @ClydeByrdIII I have kept the main thread in infinite listening mode, so i that it should not terminate. Let me tell you that I am killing the spawned thread since it's again in infinite loop. Does that change the scenario? – Yogesh lele Jul 23 '13 at 16:18
  • Can you move the declaration of your pthread_t to the inside? Unless you are trying to reuse the same pthread, I think it's better to make a new pthread per request. Casey's answer looks pretty good for what you need. – CBIII Jul 23 '13 at 16:22
  • @Yogeshlele Returning from `main()` is equivalent to calling `exit()` in every implementation I've used, doing either kills all running threads and ends the process. I'm almost certain that behavior is required, although I can't find where that is specified in the POSIX standards right now. – Casey Jul 23 '13 at 17:05
  • @Casey: One could leave `main()` via `pthread_exit()`, which *could* leave still running threads alive. Wether it would do so, might be implementation specific. – alk Jul 30 '13 at 16:44

2 Answers2

3

If you don't care about the return value from your threads, and you're not interested in when they complete, then you should detach them with pthread_detach or simply create them in a detached state to begin with:

pthread_attr_t thread_attr;
pthread_attr_init(&thread_attr);
pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED);
while(1)    //server always to be in listen mode
{
  client_len=sizeof(client_sockaddr);
  client_sockfd=accept(server_sockfd,(struct sockaddr*)&client_sockaddr,&client_len);

  pthread_t t;
  pthread_create(&t,&thread_attr,server_thread,(void*)client_sockfd);
}
pthread_attr_destroy(&thread_attr);
Casey
  • 41,449
  • 7
  • 95
  • 125
  • @simonc I'm not familiar with any implementation of Berkeley sockets in which a socket handle is anything other than an integer. Since that integer is being passed by value to the thread function - albeit in `void*` form via the cast - there's no need to worry about overwriting. There is a possibility that `void*` can't store all possible `int` values, but such implementations are rare and in any case that cast was present in the OP code - I didn't introduce it. – Casey Jul 23 '13 at 16:33
  • Sorry, you're correct. I'd mis-read and thought the address of the local variable was being passed rather than the handle being munged into a `void*`. – simonc Jul 23 '13 at 16:35
0

Add printf("check string\n"); after pthread_join in your code. compile and run it now. You might get some idea about your problem.

You will not meet printf function.

Reason for the behavior is pthread_join will wait for first created thread to finish the job.

so unless and until first thread finish the job new thread will not created. So your code will not accept any new client connection.

So don't use pthred_join inside your while(1) then your problem will be solved.

pthread_join is mostly useful when main process want to wait until thread finishes the job.

sujin
  • 2,813
  • 2
  • 21
  • 33