File:  [OS/2 SDKs] / os2sdk / demos / examples / dosexit / dosexit.c
Revision 1.1.1.2 (vendor branch): download - view: text, annotated - select for diffs
Thu Aug 9 12:26:25 2018 UTC (7 years, 9 months ago) by root
Branches: msft, MAIN
CVS tags: os2sdk-1988, HEAD
Microsoft OS/2 SDK 03-01-1988

/*
 * Example of DosExit usage.
 *
 * DosExit can be used to terminate a specific thread, or all threads in
 * a process. In general, even if your application has just one thread
 * it is very important to use the "all threads" form of DosExit when
 * your application terminates. This is because you may call some library
 * package or run under an environment manager which creates threads on
 * your behalf, which you are not aware of.
 *
 * In this example, the main thread is the first to call DosExit, but calls it
 * so as to only end the one thread. Whichever of the two remaining threads
 * first notices that the flag has been set will then call DosExit and
 * terminate the process.  If f_thread is the one to call DosExit then
 * the process will return an exit code of 1, and if it is g_thread, an
 * exit code of 2.
 *
 * compile as: cl -Gs -AL -G2 -Lp DosExit.c
 *
 * Created by Microsoft Corp. 1986
 *
 */

#define INCL_DOSPROCESS
#define INCL_SUB

#include <os2def.h>
#include <bse.h>
#include <malloc.h>

#define STACK_SIZE	1024
extern void f_thread(void);
extern void g_thread(void);
int flag;

main()
{
	char *stkptr;
	TID thread_id;
	
	flag = 0;

	/* create two subthreads */
	stkptr = (char *)malloc(STACK_SIZE) + STACK_SIZE;
	DosCreateThread(f_thread, &thread_id, stkptr);
	
	stkptr = (char *)malloc(STACK_SIZE) + STACK_SIZE;
	DosCreateThread(g_thread, &thread_id, stkptr);
	
	/* let the sub-threads run for a while */
	DosSleep(500L);
	
	/* exit from this thread only */
	printf("main thread is now exiting\n");
	flag = 1;
	DosExit(EXIT_THREAD,0);
}

void f_thread()
{
	/* wait for the global flag to be set */
	while (flag == 0) {
		VioWrtTTy("f_thread, still running\r\n", 25, 0);
		DosSleep(0L);
	}
	
	/* exit this process (all threads) */
	VioWrtTTy("f_thread, now exiting all\r\n", 27, 0);
	DosExit(EXIT_PROCESS,1);
}

void g_thread()
{
	/* wait for the global flag to be set */
	while (flag == 0) {
		VioWrtTTy("g_thread, still running\r\n", 25, 0);
		DosSleep(0L);
	}
	
	/* exit this process (all threads) */
	VioWrtTTy("g_thread, now exiting all\r\n", 27, 0);
	DosExit(EXIT_PROCESS,2);
}

unix.superglobalmegacorp.com

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