/********************************************************************************/ /* File Name : mthread.cpp */ /* Log File Name: output.txt */ /* Author : Meng Yi Yee */ /* Class : CS 431 */ /* Date : Oct 4, 1999 */ /* */ /* This program shows an approach to developing multi-threaded programs. It */ /* shows how to create a new thread by using the _beginthreadex function. The */ /* main function of this program is myFunc which will take an argument list */ /* from the _beginthreadex. This number will be used to compute a sum of 0 to */ /* current thread number. Some output messages will be printed to the screen to */ /* show that the thread is running. */ /* */ /* The main program first read the user input number of thread(s) and runtime */ /* in second(s). Then the program compute the halt time by using the specified */ /* runtime. After that, it will start creating thread(s) by using */ /* _beginthreadex function. After this, the program will loop until the local */ /* machine time equal to halt time then it halts. Lastly, the shut down time */ /* will be printed out. */ /********************************************************************************/ #include "stdafx.h" #include #include #include #include #include #include //Declare and initialize global variable static int runFlag=TRUE; /********************************************************************************/ /* Function: myFunc */ /* This function read the thread number and then compute a sum of 0 to thread */ /* number. This function also print some messages to show that the thread is */ /* running. */ /********************************************************************************/ DWORD WINAPI myFunc(LPVOID ArgList) { //Declare and initialize local variables int counter, sum; SYSTEMTIME time; GetLocalTime (&time); sum=0; //Assign the counter to the current thread number counter= (int)ArgList; //Output some messages do a sum of 0 to current thread number to show //that the thread is running printf("\nThread starting at local time %d: %d: %d\n", time.wHour, time.wMinute, time.wSecond); printf("myFunc is being activated.....\n"); printf("Thread number is %d\n", ArgList); printf("Sum of 0 to thread number: "); for (int i=0; i<=counter; ++i) { printf("%d ", i); if (i \n"); printf("Example: mthread 2 5\n"); return 0; } //Get the number of thread(s) N=atoi(argv[1]); //Get the time the thread should run, runtime is in second(s) runTime=atoi(argv[2]); GetLocalTime(&now); printf("\nCurrent Time: %d: %d: %d\n", now.wHour, now.wMinute, now.wSecond); printf("runTime: %d Second(s)\n\n", runTime); //Calculate time to halt GetLocalTime(&now); printf("mthread: Suite starting at local time %d:%d:%d\n", now.wHour, now.wMinute, now.wSecond); stopTimeSecond=(now.wSecond + (WORD)runTime)%60; stopTimeMinute=now.wMinute+(now.wSecond+(WORD)runTime)/60; //For 1 to N for (int i=0; i=stopTimeMinute)&&(now.wSecond>=stopTimeSecond)) runFlag=FALSE; Sleep(1000); } Sleep(5000); //Get the current machine time and output the program shut down time GetLocalTime(&now); printf("Shut Down Time: %d: %d: %d\n", now.wHour, now.wMinute, now.wSecond); return 0; }