DosWaitThread() Function

Where a secondary thread must complete its processing and terminate before the primary thread can continue, the primary thread may use the DosWaitThread() function to determine whether the secondary thread has terminated. This function is used in conjunction with the Presentation Manager timer facility, to periodically check whether the secondary thread has issued a DosExit() function call. An example of a secondary thread using this technique is given in Figure "Synchronization Using the DosWaitThread() Function (Part 1)".

When the secondary thread has been started, the window procedure in the primary thread stores the thread identifier in its instance data area (typically using window words), and uses the Presentation Manager timer facility to send periodic WM_TIMER messages to itself, as shown in Figure "Synchronization Using the DosWaitThread() Function (Part 2)".

Whenever it receives a WM_TIMER message, the window procedure retrieves the thread identifier from its instance data area and uses the DosWaitThread() function to determine whether the thread has terminated. If so, it performs the required processing. If the thread has not yet terminated, it immediately returns control to Presentation Manager. Note the use of the DosExit() function in Figure "Synchronization Using the DosWaitThread() Function (Part 1)". This assumes that the processing performed by the routine does not use an object window, and does not call C run-time library functions. As mentioned earlier in this chapter, secondary threads without object windows are typically used to perform a single, lengthy task, and terminate upon completion of this task. Since they are able to use the DosExit() function and the completion of their task causes the termination of the thread, such threads are ideal candidates for use of the DosWaitThread() function. For situations where the progress of execution must be indicated to the primary thread, an event semaphore is more suitable.

As already mentioned, there is very little difference between the use of the DosWaitThread() function and the use of an event semaphore. Both are used in conjunction with the Presentation Manager timer facility and in fact, both use an event semaphore. The DosWaitThread() function avoids the need for the application to explicitly open and check the semaphore, since the DosWaitThread() function performs these operations transparently. However, while an event semaphore may be used to indicate any significant event during execution of a secondary thread, while the DosWaitThread() function can only signal termination of the thread. Hence the DosWaitThread() function is slightly less flexible than the explicit use of an event semaphore with the Presentation Manager timer facility.


[Back: Timers and Semaphores]
[Next: DosWaitChild() Function]