Try this:
#include "stdafx.h"
#define _MT
#include
#include /* _beginthread, _endthread */
#include
#include
#include
#include
#pragma comment(lib, "user32.lib")
#pragma comment(lib, "libcmt.lib")
#pragma comment(linker, "/NODEFAULTLIB:libcd.lib")
void ThreadFunction1(void *ch);
void ThreadFunction2(void *dummy);
BOOL repeat = TRUE; /* Global repeat flag */
int main(){
printf("\nMultithreading with two threads.\n\n");
printf("Thread Function 2 listens for key strokes. \n");
printf("Thread Function 1 does all the other work \n");
printf(" \n");
CHAR ch = 'A';
/\* Launch ThreadFunction2 thread to check for terminating keystroke. \*/
\_beginthread(ThreadFunction2, 0, NULL);
/\* Loop until ThreadFunction2 terminates program. \*/
while (repeat){
/\* On first loops, launch character threads. \*/
\_beginthread(ThreadFunction1, 0, (void \*)(ch++));
/\* Wait one second between loops. \*/
Sleep(1000L);
}
printf(" \\n");
return 0;
}
/* ThreadFunction2 - Thread to wait for a keystroke, then end program. */
void ThreadFunction2(void *dummy){
_getch();
repeat = 0; /* _endthread implied */
}
/* ThreadFunction2 - Thread to do work */
void ThreadFunction1(void *ch){
while (repeat){
/\* Pause between loops. \*/
Sleep(100L);
}
/\* \_endthread given to terminate \*/
\_endthread();
}