Annotation of os2sdk/demos/examples/dosexit/dosexit.c, revision 1.1.1.2

1.1       root        1: /*
1.1.1.2 ! root        2:  * Example of DosExit usage.
1.1       root        3:  *
1.1.1.2 ! root        4:  * DosExit can be used to terminate a specific thread, or all threads in
1.1       root        5:  * a process. In general, even if your application has just one thread
1.1.1.2 ! root        6:  * it is very important to use the "all threads" form of DosExit when
1.1       root        7:  * your application terminates. This is because you may call some library
                      8:  * package or run under an environment manager which creates threads on
                      9:  * your behalf, which you are not aware of.
                     10:  *
1.1.1.2 ! root       11:  * In this example, the main thread is the first to call DosExit, but calls it
1.1       root       12:  * so as to only end the one thread. Whichever of the two remaining threads
1.1.1.2 ! root       13:  * first notices that the flag has been set will then call DosExit and
        !            14:  * terminate the process.  If f_thread is the one to call DosExit then
1.1       root       15:  * the process will return an exit code of 1, and if it is g_thread, an
                     16:  * exit code of 2.
                     17:  *
1.1.1.2 ! root       18:  * compile as: cl -Gs -AL -G2 -Lp DosExit.c
1.1       root       19:  *
1.1.1.2 ! root       20:  * Created by Microsoft Corp. 1986
1.1       root       21:  *
                     22:  */
                     23: 
1.1.1.2 ! root       24: #define INCL_DOSPROCESS
        !            25: #define INCL_SUB
        !            26: 
        !            27: #include <os2def.h>
        !            28: #include <bse.h>
1.1       root       29: #include <malloc.h>
                     30: 
                     31: #define STACK_SIZE     1024
                     32: extern void f_thread(void);
                     33: extern void g_thread(void);
                     34: int flag;
                     35: 
                     36: main()
                     37: {
                     38:        char *stkptr;
1.1.1.2 ! root       39:        TID thread_id;
1.1       root       40:        
                     41:        flag = 0;
                     42: 
                     43:        /* create two subthreads */
                     44:        stkptr = (char *)malloc(STACK_SIZE) + STACK_SIZE;
1.1.1.2 ! root       45:        DosCreateThread(f_thread, &thread_id, stkptr);
1.1       root       46:        
                     47:        stkptr = (char *)malloc(STACK_SIZE) + STACK_SIZE;
1.1.1.2 ! root       48:        DosCreateThread(g_thread, &thread_id, stkptr);
1.1       root       49:        
                     50:        /* let the sub-threads run for a while */
1.1.1.2 ! root       51:        DosSleep(500L);
1.1       root       52:        
                     53:        /* exit from this thread only */
                     54:        printf("main thread is now exiting\n");
                     55:        flag = 1;
1.1.1.2 ! root       56:        DosExit(EXIT_THREAD,0);
1.1       root       57: }
                     58: 
                     59: void f_thread()
                     60: {
                     61:        /* wait for the global flag to be set */
                     62:        while (flag == 0) {
1.1.1.2 ! root       63:                VioWrtTTy("f_thread, still running\r\n", 25, 0);
        !            64:                DosSleep(0L);
1.1       root       65:        }
                     66:        
                     67:        /* exit this process (all threads) */
1.1.1.2 ! root       68:        VioWrtTTy("f_thread, now exiting all\r\n", 27, 0);
        !            69:        DosExit(EXIT_PROCESS,1);
1.1       root       70: }
                     71: 
                     72: void g_thread()
                     73: {
                     74:        /* wait for the global flag to be set */
                     75:        while (flag == 0) {
1.1.1.2 ! root       76:                VioWrtTTy("g_thread, still running\r\n", 25, 0);
        !            77:                DosSleep(0L);
1.1       root       78:        }
                     79:        
                     80:        /* exit this process (all threads) */
1.1.1.2 ! root       81:        VioWrtTTy("g_thread, now exiting all\r\n", 27, 0);
        !            82:        DosExit(EXIT_PROCESS,2);
1.1       root       83: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.