-
March 16th, 2011, 12:00 PM
#1
Check for running HANDLE
As the topic say's, I'm trying to find out how to test if a thread HANDLE is already in use. Or more simply, if a thread is already running. What if condition would I need to test to check if a thread is already in use? And how would I prevent a new thread from starting if it already exists? Thanx.
-
March 16th, 2011, 04:12 PM
#2
Re: Check for running HANDLE
Thread is running:
Code:
WAIT_TIMEOUT == WaitForSingleObject(hThread, 0);
And I hardly can understand you question about preventing from starting. Could you please rephrase.
Best regards,
Igor
-
March 16th, 2011, 06:42 PM
#3
Re: Check for running HANDLE
The easiest way for that you would use the good old static BOOL yourself in your code
static BOOL ThreadLaunched = FALSE;
// When you launch the thread
ThreadLaunched = TRUE;
// In the thread code just before it terminates and returns (0)
ThreadLaunched = FALSE;
ThreadLaunched hence tells you if the thread is running.
-
March 17th, 2011, 02:29 AM
#4
Re: Check for running HANDLE
>> The easiest way ...
That's the incorrect way since there's no synchronization. More than one thread can not access the same memory location, where at least one thread is writing, without using synchronization primitives provided by your threading library.
A thread handle will be in a signaled state when it is not longer running. So if it isn't signaled, then it's running (condition in post #2).
gg
-
March 19th, 2011, 04:26 PM
#5
Re: Check for running HANDLE
Originally Posted by dellthinker
As the topic say's, I'm trying to find out how to test if a thread HANDLE is already in use. Or more simply, if a thread is already running. What if condition would I need to test to check if a thread is already in use? And how would I prevent a new thread from starting if it already exists? Thanx.
Use the GetExitCodeThread api. In fact, while you are in msdn looking at this function, check out all the related thread api's. Read through them and understand what they do because even if you don't need them today, you're likely to remember something about them and you can find them in msdn in the future.
For the second question, it's all about having good programming practices - which means initializing all variables, cleaning up the variables when necessary and setting the variables back to null.
In multithreaded programming there are a few things to keep track of mainly handles for threads, events, mutexes, or critical section objects. All of these need to be initialized and cleaned up after use.
A great way to do this is to use classes and initialize the entities to NULL in the constructor and free them in the destructor.
Say your class has Start and Stop methods. If you initialize the thread handle to NULL in the constructor, it's a simple matter of checking it for NULL in the Start method before starting a thread.
Threading can be extremely easy once the programmer follows a few best practices. Some programmers seem to go out of their way to make it hard.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|