I have multithreaded programming. And my worker thread is a ‘while(1)’ and there is no mechanism that makes it come out of this loop.
So what is the best way to terminate the thread when application finishes execution?
Thanks
I have multithreaded programming. And my worker thread is a ‘while(1)’ and there is no mechanism that makes it come out of this loop.
So what is the best way to terminate the thread when application finishes execution?
Thanks
well. I use a loop that is dependent on a flag like:
while(!exit_flag) {}
and in main i set the exit flag to true, when i want the thread to quit.
% gcc -v
Configured with: FreeBSD/i386 system compiler
Thread model: posix
gcc version 3.3.3 [FreeBSD] 20031106
Thanks,
I am using WIndows NT operating system.
>And anyway, if your program terminates, all threads will terminate as well.
But I am not sure of this..
not sure if this is what your trying to do
but If you just want to exit your thread at some time try
this will exit the thread but not the rest of the program.Code:pthread_exit(NULL);
The NULL is used instead of passing in some variable that could store the threads exit info, I think.
> while(!exit_flag) {}
> while(1) {}
Is a good solution, how else are you going to keep the thread alive. Directly a thread exits the thread function its going to die!
A thread will be killed off when the main thread dies.
if you want it to die in the while(1) either use break; or return; or use a condition such as !exit_flag.
So, if I use while(1) like in the following example that wrong is it?
Code:unsigned int WINAPI ComPortThread(LPVOID lpvThreadParam) { char szBuffer[10]; hComPortHandle = CreateFile("COM1", GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL); while(1) { ReadFile(hComPortHandle, &szBuffer, 10, (unsigned long*)&iNoOfBytesRead, NULL); SendMessage(hwnd, COM_MESSAGE, NULL, (LPARAM)&szBuffer); } return TRUE; }
Last edited by Scarlet7; 05-05-2003 at 03:23 PM.
I want achieve following in worker thread
If buffer content is not NULL, then send that buffer content over serial line or if time elapsed since last string sent over serial line is more than 5 seconds, then send one default string on serial line.
I know, I can use WaitForSingleObject, that takes handle and waitTime as parameters. But how will this API treat string as HANDLE, so that I can use this API as????
WaitForSingleObject( HANDLE to string, 5 seconds)
Please not that buffer is shared between, main thread and worker thread and is just character array.
Currently, my worker thread logic(pseudocode) is
I am using time function to meaure the time.Code:Infinite loop { while(1) { if ( buffer is not empty ) // just read so no guarding Acquire mutex Sendbuffer = buffer Release Mutex break; else if ( 5 seconds elapsed) // send default command Sendbuffer = “default command” break; } Log into the file Send the command on serial line receive the response from serial line Log to the file }
Current design involves busy wait If I can treat string(buffer) as HANDLE, I can make use of WaitForSingleObject function.
Any comment on how can I make strring as HANDLE.
Thanks