- Problem with simple multithreaded console program
- Posted by Dietmar Respondek on June 25th, 2003
Hi,
I wrote a little console application to make my first steps with
multithreaded application. I use beginthread to create two new threads,
I have a global variable to stop the threads, I have one thread waiting
for a keyboard hit and sending the term signal to the other threads, one
makes an output to the console and the main threads waits for all other
threads before it exits. My Problem is from time to time the console
Windows will still be present on the desktop, but the task in the task
manager is not be present any more. Windows will not shutdown, because
its waiting for the console to exit......
I have no idea, because if I monitor the behavior in the task manager I
see as many Threads and Processes after the application exited as before.
Any ideas?
Thanks Dietmar
- Posted by Sin on June 25th, 2003
How do you "wait" for the threads to exit? You should be using
WaitForMultipleObjects() on all the handles returned by beginthread...
Alternatively, you could also use WaitForSingleObject on everyone of them in
a sequence...
In those threads, what do you consider exiting? A thread stops when you exit
the thread function (by using return, or simply getting to the end of the
code). Do not use exit() as it will shutdown the whole application. Do not
use TerminateThread from inside the thread you're trying to kill either, not
sure if it would do wrong but there simply is no point to it AFAIK...
Tell us more... Ideally if you could reproduce the problem in a mimimalist
code piece you should send it here (in the message body, not as an
attachment...).
Alex.
- Posted by Dietmar Respondek on June 25th, 2003
Sin wrote:
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <process.h>
#include <conio.h>
#include "Globals.h"
void CheckKeyboard (void*);
void OutputScreen (void*);
int main (void)
{
Thread_ID.MainWorker=_threadid;
if((Thread_ID.WorkerOne=_beginthread (CheckKeyboard, 0, NULL)) == -1)
{
printf("Error during Startup of the 1. Worker Thread!\n");
bRun=FALSE;
_exit(-1);
}
if((_beginthread (OutputScreen, 0, NULL)) == -1)
{
printf("Error during Startup of the 2. Worker Thread!\n");
bRun=FALSE;
_exit(-1);
}
while (bRun)
{
iCount++;
Sleep(750);
}
printf("Main waiting for Child Threads to exit.........");
while (Thread_ID.NoThreads != 0)
{
_asm Pause;
}
printf("done\n");
printf("Main exits.....................................done\n") ;
return 0;
}
void CheckKeyboard (void* dummy)
{
Thread_ID.WorkerOne=_threadid;
Thread_ID.NoThreads++;
printf ("Keyboard Thread(ID=0x%X) running.........\n",Thread_ID.WorkerOne);
_getch();
bRun=FALSE;
printf("Keyboard Thread(ID=0x%X) exiting.........\n",Thread_ID.WorkerOne);
Thread_ID.NoThreads--;
_endthread();
}
void OutputScreen (void* dummy)
{
Thread_ID.WorkerTwo=_threadid;
Thread_ID.NoThreads++;
printf ("Output Thread(ID=0x%X) running.........\n",Thread_ID.WorkerTwo);
while (bRun)
{
printf ("Main Thread(ID=0x%X) runs in Loop No:
%d\n",Thread_ID.MainWorker, iCount);
Sleep(1000);
}
printf("Output Thread(ID=0x%X) exiting.........\n",Thread_ID.WorkerTwo);
Thread_ID.NoThreads--;
_endthread();
}