multithreading - Making unknown number of child threads inside parent thread using win32 in C? -


how can make unknown number of child threads inside parent thread , wait each of them 1 one using win32 in c?
parent thread life time infinite , wait request , if received request make new child thread request e.g. servers .
searching web cant find .
tutorial , information appreciated .
lot , luck .

note :

1 . example imagine simple server : when user send request server server make new thread user , wait thread terminated if user send request server make thread separate old 1 , server must wait new thread separate old 1 terminate .

2 . main thread scan global array size of constant n in infinite loop , if find specific value in each of array's block run new thread operation on block's information , after thread become terminate update block's information . parent thread life time infinite because has infinite loop .

you'ld create each thread createthread , store thread handle in dynamic list or array. if want main thread recognize when thread terminated, can call waitformultipleobjects, providing array thread handles. return value of waitformultipleobjects tell thread handles signalled, thread terminated. don't forget closehandle thread handle @ end.

if want spawn threads , main thread not need know when threads terminate, can create threads createthread , close thread handle. thread's resources freed when thread terminates.

in main thread need check if receive client request. if have interface client can wait on event, add event event array passed waitformultipleobjects. if not have event in case 2, might consider calling waitformultipleobjects timeout waitformultipleobjects either returns when thread terminated or when timeout occured. in both cases main loop keeps running , can check if need spawn thread.

here pseudo code when using event client requests:

initialize empty list of thread data (thread handles , other data each thread); for(;;) {     create array big enough request event , thread handles;     a[0] = request event handle;     a[1..n] = thread handles list;     dword ret = waitformultiobjects(n+1, a, false, infinite);     if(ret == wait_object_0) {         create thread , store it's handle , other data in list;     }     else if(wait_object_0 + 1 <= ret  &&  ret <= wait_object_0 + n) {         thread (ret - wait_object_0 - 1) terminated, cleanup , don't forget closehandle();     }     else         error occured, should not happen; } 

if don't have event client requests, need poll:

initialize empty list of thread data (thread handles , other data each thread); for(;;) {     create array big enough request event , thread handles;     a[0..n-1] = thread handles list;     dword ret = waitformultiobjects(n, a, false, desired timeout);     if(ret != wait_timeout)         ; // timeout occured, no thread terminated yet, nothing here     else if(wait_object_0 <= ret  &&  ret < wait_object_0 + n) {         thread (ret - wait_object_0) terminated, cleanup , don't forget closehandle();     }     else         error occured, should not happen;     // check client requests, not in case of wait_timeout.     // otherwise might run situation every time call waitformultiobjects thread ended before timeout occured , recognize after lot of loop runs when last thread terminated.     if(there client request) {         create thread , store it's handle , other data in list;     } } 

if not need store data each thread, can store thread handles , maybe store them in big array. eliminate step build array thread handle list.


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -