--- os2sdk/demos/examples/threads/threads.c 2018/08/09 12:25:13 1.1.1.1 +++ os2sdk/demos/examples/threads/threads.c 2018/08/09 12:26:18 1.1.1.2 @@ -1,12 +1,12 @@ /* - * DOSCREATETHREAD example. + * DosCreateThread example. * - * Copyright (C) Microsoft Corp. 1986 + * Created by Microsoft Corp. 1986 * * This example illustrates the use of threads and ram semaphores. * Two threads are given alternating access to a shared resource, in this * case, the screen. In this example a semaphore is used to protect - * the shared resource. See the DOSCRITSEC example for a different method. + * the shared resource. See the DosCritSec example for a different method. * * See example program "asmexmpl" for a version of this program in assembler. * @@ -16,7 +16,7 @@ * - C functions can be used as thread bodies. The function must have * no arguments. * - * - The thread function must end with a call to DOSEXIT to terminate + * - The thread function must end with a call to DosExit to terminate * either that thread or all threads. Simply returning, or falling * off the end will lead to unpredictable results. * @@ -52,92 +52,97 @@ * through modifying the LONG, it is left in an inconsistent state. */ +#define INCL_DOSPROCESS +#define INCL_DOSSEMAPHORES +#define INCL_DOSFILEMGR + +#include +#include #include +#undef NULL #include -#include -#include -#define HELLO0 "thread zero hello\r\n" -#define HELLO1 "THREAD ONE HELLO\r\n" +#define HELLO0 "thread zero hello\r\n" +#define HELLO1 "THREAD ONE HELLO\r\n" -#define THREAD_STK_SIZE 2048 /* thread stack size */ +#define THREAD_STK_SIZE 400 /* thread stack size */ -unsigned long sem; /* ram semaphore */ +HSEM sem; /* ram semaphore */ main(argc, argv) int argc; char *argv[]; { - void far thread1(); /* address where thread1 gets control */ - char far *thread1Stack; /* far pointer to thread1 stack */ - unsigned thread1ID; /* thread ID */ - unsigned rc; /* return code */ + void far thread1(); /* address where thread1 gets control */ + PSZ thread1Stack; /* far pointer to thread1 stack */ + TID thread1ID; /* thread ID */ + unsigned rc; /* return code */ - sem = 0L; /* init semaphore */ + sem = 0L; /* init semaphore */ - /* allocate stack space for thread 1 */ - /* since this is written in C, DOSALLOCSEG cannot be used here */ + /* allocate stack space for thread 1 */ + /* since this is written in C, DosAllocSeg cannot be used here */ - if ((thread1Stack = malloc(THREAD_STK_SIZE)) == NULL) { + if ((thread1Stack = malloc(THREAD_STK_SIZE)) == NULL) { - printf("thread stack malloc failed\n"); - DOSEXIT(1, 1); /* terminate all threads and return error */ - } - thread1Stack += THREAD_STK_SIZE; /* since stack grows down */ + printf("thread stack malloc failed\n"); + DosExit(EXIT_PROCESS, 1); /* term all threads,return error */ + } + thread1Stack += THREAD_STK_SIZE; /* since stack grows down */ - /* start another thread */ - if (rc = DOSCREATETHREAD( thread1, (unsigned far *) &thread1ID, - thread1Stack )) { + /* start another thread */ + if (rc = DosCreateThread( thread1, &thread1ID, + thread1Stack )) { - printf("create of thread failed, error: %d\n", rc); - DOSEXIT(1, 1); - } + printf("create of thread failed, error: %d\n", rc); + DosExit(EXIT_PROCESS, 1); + } - thread0(); /* this will loop forever */ + thread0(); /* this will loop forever */ - DOSEXIT(1, 0); /* Terminate all threads and exit */ + DosExit(EXIT_PROCESS, 0); /* Terminate all threads and exit */ } -thread0() /* Thread 0 */ +thread0() /* Thread 0 */ { - unsigned BytesWrit; + USHORT BytesWrit; for (;;) { - /* claim the resource - in this case, the screen */ + /* claim the resource - in this case, the screen */ - DOSSEMREQUEST( (long) &sem, -1L ); /* no timeout */ + DosSemRequest( &sem, -1L ); /* no timeout */ - /* write to the screen */ + /* write to the screen */ - DOSWRITE( 1, (char far *) HELLO0, sizeof(HELLO0) - 1, - (unsigned far *) &BytesWrit ); + DosWrite( 1, (char far *) HELLO0, sizeof(HELLO0) - 1, + &BytesWrit ); - DOSSEMCLEAR( (unsigned long) &sem ); /* release resource */ + DosSemClear( &sem ); /* release resource */ - DOSSLEEP(2000L); /* give up the processor */ + DosSleep(2000L); /* give up the processor */ } } void far -thread1() /* Thread 1 */ +thread1() /* Thread 1 */ { - unsigned BytesWrit; + USHORT BytesWrit; for (;;) { - /* claim the resource - in this case, the screen */ + /* claim the resource - in this case, the screen */ - DOSSEMREQUEST( (unsigned long) &sem, -1L ); /* no timeout */ + DosSemRequest( &sem, -1L ); /* no timeout */ - /* write to the screen */ + /* write to the screen */ - DOSWRITE(1, (char far *)HELLO1, sizeof(HELLO1) - 1, - (unsigned far *) &BytesWrit ); + DosWrite(1, (char far *)HELLO1, sizeof(HELLO1) - 1, + &BytesWrit ); - DOSSEMCLEAR( (unsigned long) &sem ); /* release resource */ + DosSemClear( &sem ); /* release resource */ - DOSSLEEP(2000L); /* give up the processor */ + DosSleep(2000L); /* give up the processor */ } }