An application might need to ensure that one thread has finished executing before another thread continues with its own task. For example, one thread might have to finish reading a disk file into memory before another thread can use the information. You can use DosWaitThread to suspend a thread until another thread has ended.
DosWaitThread places the current thread into a wait state until another thread in the current process has ended. It then returns the thread identifier of the ending thread.
The following code fragment creates three threads. The thread identifier for each thread is returned by DosCreateThread in the atid array. Using &atid[0] as a parameter in the call to DosWaitThread causes OS/2 to wait until the thread with that identifier (the thread running Thread2Func) ends.
#define INCL_DOSPROCESS /* Process and thread values */ #include <os2.h> #define HF_STDOUT 1 /* Standard output handle */ TID tidAtid[3]; ULONG ulWritten; DosCreateThread(&tidAtid[0], Thread2Func, 0, 0, 4096); DosCreateThread(&tidAtid[1], Thread3Func, 0, 0, 4096); DosCreateThread(&tidAtid[2], Thread4Func, 0, 0, 4096); DosWaitThread(&tidAtid[0], DCWW_WAIT); DosWrite(HF_STDOUT, "The thread has ended\r\n", 27, &ulWritten);
If you set the tid parameter to 0 in the call to DosWaitThread, OS/2 waits only until the next thread (any thread in the process) ends. The identifier for the ended thread is then returned in the tid parameter.
After the threads are created as in the preceding example, the following code fragment waits until one of the threads ends, and then returns its thread identifier:
#define INCL_DOSPROCESS /* Process and thread values */ #include <os2.h> TID tid; tid = 0; DosWaitThread(&tid, DCWW_WAIT);
The thread identifier of the next thread to end after the DosWaitThread call is returned in the tid parameter.
You can use DosWaitThread so that you can recover thread resources when the thread ends, or to synchronize the execution of a thread with the execution of other threads.