|
|
1.1 ! root 1: /* ! 2: * Example of DOSEXIT usage. ! 3: * ! 4: * DOSEXIT can be used to terminate a specific thread, or all threads in ! 5: * a process. In general, even if your application has just one thread ! 6: * it is very important to use the "all threads" form of DOSEXIT when ! 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: * ! 11: * In this example, the main thread is the first to call DOSEXIT, but calls it ! 12: * so as to only end the one thread. Whichever of the two remaining threads ! 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 ! 15: * the process will return an exit code of 1, and if it is g_thread, an ! 16: * exit code of 2. ! 17: * ! 18: * compile as: cl -Gs -AL -G2 -Lp dosexit.c ! 19: * ! 20: * Copyright (C) Microsoft Corp. 1986 ! 21: * ! 22: */ ! 23: ! 24: #include <malloc.h> ! 25: #include <doscalls.h> ! 26: #include <subcalls.h> ! 27: ! 28: #define STACK_SIZE 1024 ! 29: extern void f_thread(void); ! 30: extern void g_thread(void); ! 31: int flag; ! 32: ! 33: main() ! 34: { ! 35: char *stkptr; ! 36: unsigned thread_id; ! 37: ! 38: flag = 0; ! 39: ! 40: /* create two subthreads */ ! 41: stkptr = (char *)malloc(STACK_SIZE) + STACK_SIZE; ! 42: DOSCREATETHREAD(f_thread, &thread_id, stkptr); ! 43: ! 44: stkptr = (char *)malloc(STACK_SIZE) + STACK_SIZE; ! 45: DOSCREATETHREAD(g_thread, &thread_id, stkptr); ! 46: ! 47: /* let the sub-threads run for a while */ ! 48: DOSSLEEP(500L); ! 49: ! 50: /* exit from this thread only */ ! 51: printf("main thread is now exiting\n"); ! 52: flag = 1; ! 53: DOSEXIT(0,0); ! 54: } ! 55: ! 56: void f_thread() ! 57: { ! 58: /* wait for the global flag to be set */ ! 59: while (flag == 0) { ! 60: VIOWRTTTY("f_thread, still running\r\n", 25, 0); ! 61: DOSSLEEP(0L); ! 62: } ! 63: ! 64: /* exit this process (all threads) */ ! 65: VIOWRTTTY("f_thread, now exiting all\r\n", 27, 0); ! 66: DOSEXIT(1,1); ! 67: } ! 68: ! 69: void g_thread() ! 70: { ! 71: /* wait for the global flag to be set */ ! 72: while (flag == 0) { ! 73: VIOWRTTTY("g_thread, still running\r\n", 25, 0); ! 74: DOSSLEEP(0L); ! 75: } ! 76: ! 77: /* exit this process (all threads) */ ! 78: VIOWRTTTY("g_thread, now exiting all\r\n", 27, 0); ! 79: DOSEXIT(1,2); ! 80: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.