简体   繁体   中英

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

How can I make unknown number of child threads inside parent thread and wait for each of them one by one using win32 in C?
The parent thread life time is infinite and it is wait for request and if received any request then make a new child thread for that request eg like servers .
I am searching the web but I cant find anything .
Any tutorial and information appreciated .
Thanks a lot , good luck .

note :

1 . For example imagine a simple server : When a user send a request to that server the server make a new thread for that user and wait for that thread to terminated but if another user send another request the server make another thread which is completely separate from the old one and then the server must wait for the new thread separate from the old one to terminate .

2 . The main thread scan an global array with the size of constant n in the infinite loop and if find the specific value in each of array's block then run the new thread to do some operation on that block's information and then after the thread become terminate update that block's information . The parent thread life time is infinite because it has a infinite loop .

Maybe yo should use the WaitForMultipleObjects function.

http://msdn.microsoft.com/en-us/library/windows/desktop/ms687025(v=vs.85).aspx

You'ld create each thread with CreateThread and store the thread handle in a dynamic list or array. If you want the main thread to recognize when a thread terminated, then you can call WaitForMultipleObjects , providing it an array with all thread handles. The return value of WaitForMultipleObjects will tell you which thread handles was signalled, so which thread terminated. Don't forget to CloseHandle the thread handle at the end.

If you just want to spawn threads and the main thread does not need to know when the threads terminate, then you can just create the threads with CreateThread and close the thread handle. The thread's resources will be freed when the thread terminates.

In your main thread you will also need to check if you receive a client request. If you have an Interface to the client where you can wait on an event, then just add the event to the event array passed to WaitForMultipleObjects . If you do not have an event like in your case 2, then you might consider calling WaitForMultipleObjects with a timeout so WaitForMultipleObjects either returns when a thread terminated or when the timeout occured. In both cases your main loop keeps running and you can check if you need to spawn another thread.

Here is some pseudo code when using an event for the client requests:

initialize empty list of thread data (thread handles and other data for each thread);
for(;;) {
    create an array a big enough for the request event and for all thread handles;
    a[0] = request event handle;
    a[1..n] = thread handles from the list;
    DWORD ret = WaitForMultiObjects(n+1, a, FALSE, INFINITE);
    if(ret == WAIT_OBJECT_0) {
        create thread and store it's handle and other data in the list;
    }
    else if(WAIT_OBJECT_0 + 1 <= ret  &&  ret <= WAIT_OBJECT_0 + n) {
        thread (ret - WAIT_OBJECT_0 - 1) terminated, do your cleanup and don't forget CloseHandle();
    }
    else
        error occured, should not happen;
}

If you don't have an event for the client requests, then you need to poll:

initialize empty list of thread data (thread handles and other data for each thread);
for(;;) {
    create an array a big enough for the request event and for all thread handles;
    a[0..n-1] = thread handles from the list;
    DWORD ret = WaitForMultiObjects(n, a, FALSE, your desired timeout);
    if(ret != WAIT_TIMEOUT)
        ; // timeout occured, no thread terminated yet, nothing to do here
    else if(WAIT_OBJECT_0 <= ret  &&  ret < WAIT_OBJECT_0 + n) {
        thread (ret - WAIT_OBJECT_0) terminated, do your cleanup and don't forget CloseHandle();
    }
    else
        error occured, should not happen;
    // Always check for client requests, not only in case of WAIT_TIMEOUT.
    // Otherwise you might run into the situation that every time you call WaitForMultiObjects a thread ended just before the timeout occured and you only recognize it after a lot of loop runs when the last thread terminated.
    if(there is a client request) {
        create thread and store it's handle and other data in the list;
    }
}

If you do not need to store extra data for each thread, then you can just store the thread handles and maybe already store them in a big array. Thus you could eliminate the step to build an array from the thread handle list.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM