Creating a Secondary Thread

In Application Structure, it is mentioned that an application must create its own input message queue to process messages intended for its windows. The Presentation Manager message-handling implementation creates message queues on a per-thread basis, and thus requires that any thread that creates a window (whether that window is a display window or an object window) and processes messages must have its own message queue.

The primary thread of an application is typically a user interface thread that handles processing for display windows on the screen; this thread creates the application's main message queue and processes messages caused by user interaction. The primary thread may also create a secondary thread to deal with messages that cause lengthy processing to be carried out, leaving the primary thread free to respond to user input. A secondary thread may be created in one of two ways:

  • The _beginthread() function provided by the C compiler should be used to create secondary threads that will contain object windows, or that contain code which calls C run-time library functions. This function maintains certain internal C library control data that is, by default, not maintained by the DosCreateThread() function.

  • The DosCreateThread() function provided by OS/2 may be used to create secondary threads that will not contain object windows, and that do not call C run-time library functions. The DosCreateThread() function offers a slight performance advantage over the _beginthread() function.

    The _beginthread() function is used since it establishes internal semaphores to serialize access to the run-time library's global data and non-reentrant library functions, transparently to the calling application. The _beginthread() function also maintains information for each thread, such as the exception handling environment and the calling address for reentrant functions. Since window procedures are reentrant, use of DosCreateThread() in such situations may cause execution errors.

    Whenever a thread is created, a thread information block (TIB) is created by the operating system. The TIB contains information such as the thread ID, priority and stack size. This information may be accessed by the application using the DosGetInfoBlocks() function. This function also returns a pointer to information on the thread's parent process, which resides in the process information block (PIB). The DosGetInfoBlocks() function is described in the IBM OS/2 Version 2.0 Control Program Reference.


    [Back: Multitasking Considerations]
    [Next: Threads Containing Object Windows]