You use DosCreateThread to create a new thread for a process.

DosCreateThread requires the address of the code to execute and a variable to receive the identifier of the thread. The address of the code is typically the address of a function that is defined within the application.

You can pass one ULONG parameter to the thread when you start it. To pass more information to the thread, pass the address of a data structure.

You specify how you want the thread to run when you call DosCreateThread. If bit 1 of the flag parameter in the function call is 0, the thread starts immediately. If bit 1 of the flag parameter is 1, the thread starts suspended and will not run until the application calls DosResumeThread.

Each thread maintains its own stack. You specify the size of the stack when you call DosCreateThread. The amount of space needed depends on a number of factors, including the number of function calls the thread makes and the number of parameters and local variables used by each function. If you plan to call OS/2 functions, a reasonable stack size is 8192 bytes (8KB); 4096 bytes (4KB) should be the absolute minimum. If bit 1 of the flag parameter is 0, OS/2 uses the default method for initializing the thread's stack. If bit 1 of the flag parameter is 1, memory for the thread's entire stack is pre-committed.

The following code fragment creates a thread:

    #define INCL_DOSPROCESS         /* Process and thread values */
    #include <os2.h>
    #include <stdio.h>

    #define HF_STDOUT 1             /* Standard output handle    */

    VOID _System ThreadFunc(ULONG ulBeepLen);

    INT main(VOID)
      {
      ULONG ulBeepLen;
      TID   tidThread;

      ulBeepLen = 1000;
      DosCreateThread(&tidThread,       /* Thread ID returned by DosCreateThread */
                      &ThreadFunc,      /* Address of the thread function        */
                      ulBeepLen,        /* Parameter passed to thread            */
                      0,                /* Immediate execution, default stack    */
                                        /* initialization                        */
                      4096);            /* Stack size                            */

      DosWaitThread(&tidThread,
                    DCWW_WAIT);
      return 0;
      } /* end main */

/***************************************************/
/* ThreadFunc                                      */
/***************************************************/
    VOID _System ThreadFunc(ULONG ulBeepLen)
      {
      ULONG ulWritten; /* needed for DosWrite */

      DosBeep(750, ulBeepLen);

      DosWrite(HF_STDOUT,
               "Message from new thread\r\n",
               25,
               &ulWritten);

      DosExit(EXIT_PROCESS, 0);
      } /* end ThreadFunc */

A thread continues to run until it calls DosExit, returns control to OS/2, or is ended by a DosKillThread call.

In the preceding example, the thread exits when the function implicitly returns control at the end of the function.


[Back] [Next]