Operating System - HP-UX
1833325 Members
2980 Online
110051 Solutions
New Discussion

Re: child thread blocking parent

 
SOLVED
Go to solution
Julian Lurie
New Member

child thread blocking parent

I am trying to write a simple multithreaded sockets server.
pthread_create returns 0 and creates and excutes the child thread ok.
Whilst the child thread is executing though , itseems to block the parent thread from continuing (which I don't think it should do) and from accepting a further connection.
Only when the child thread has finished executing , is the parent able to continue executing and accept another connection.

I am compiling : cc -Ae -D_POSIX_C_SOURCE=199506L -o stst.exe stst.c -lpthread on an HP=UX 11i

Any advice on how to prevent the child thread from blocking the parent would be appreciated.

Julian
3 REPLIES 3
Bruno Vidal
Respected Contributor

Re: child thread blocking parent

Hi,
It is normal !!! In your code, you do a pthread_create, and then a pthread_join. Take a look in man page, this function wait for the completion of the thread !! So you primary thread launch a thread, and wait that this thread has finished. Why do you put this pthread_join at this stage ? It should be somewhere else to wait the end of each thread, not in your launching function.

Cheers.
Umapathy S
Honored Contributor

Re: child thread blocking parent

Julian,
pthread_join() will wait for the thread it created. It is blocking and waiting for the thread to be finished. So, whatever you are experiencing is correct.

You are also calling pthread_detach(). This you have to do after creating the thread. Then write the code for the parent thread. Call pthread_join() at the end of the show to let the child thread join parent thread.

Read the man pages of pthread_join, pthread_create and pthread_detach for more info.

HTH,
Umapathy
Arise Awake and Stop NOT till the goal is Reached!
Umapathy S
Honored Contributor
Solution

Re: child thread blocking parent

Julian,
The fix will be like this.

Whenever you are creating the thread, store the thread_handle in a datastructure. When the socket server goes down, go in a loop for the number of threads created so far and call pthread_join() with those thread handles to wait for the threads one by one.

HTH,
Umapathy
Arise Awake and Stop NOT till the goal is Reached!