Changing the Priority of a Thread

You can use DosSetPriority to change the execution priority of threads in a process. The execution priority defines when or how often a thread receives an execution time slice. Threads with higher priorities receive time slices before those with lower priorities. When a thread that is higher in priority than the currently running thread becomes ready to run, it immediately preempts the lower priority thread (the lower priority thread does not get to complete its time slice). Threads with equal priority receive time slices in a round-robin order. If you raise the priority of a thread, the thread is executed more frequently.

You can use DosSetPriority to set the priority for one thread in a process, for all threads in a process (and thus the process itself), or for threads in a child process.

A process can change the priority of any thread within itself. When a process changes the priority of threads in a descendant process, however, only those with default priorities are changed. The priority of any thread in a descendant process that has already explicitly changed its priority from the default with DosSetPriority is not changed.

In the following code fragment, DosSetPriority lowers the priority of a process to be used as a background process:

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

    PTIB ptib;    /* thread information block          */
    PPIB ppib;    /* process information block         */
    APIRET rc;    /* return code from DosGetInfoBlocks */

    rc = DosGetInfoBlocks(&ptib,
                          &ppib);

    DosSetPriority(PRTYS_PROCESSTREE,
                   PRTYC_IDLETIME,
                   0,
                   ppib->pib_ulpid);

DosGetInfoBlocks retrieves the process information blocks and thread information blocks. DosSetPriority then uses the process identifier to change the priority to idle time (idle-time processes receive the least attention by OS/2).

If you specify PRTYS_PROCESS when calling DosSetPriority, only the priority of the specified process changes. The priorities of all child processes remain unchanged.

If you specify PRTYS_THREAD in the call to DosSetPriority, you must specify a thread identifier as the last parameter. The priority of the specified thread changes, but the priorities of all other threads in the process remain unchanged.

Whenever DosSetPriority is called with a class specification, but no value is specified for priority-delta, the base priority level defaults to 0.


[Back: Obtaining Information about a Thread]
[Next: Suspending the Current Thread]