We help IT Professionals succeed at work.

All or nothing

Lite
Lite asked
on
How to make a group of statements act as one instruction? All of them are executed without being interrupted by other thread or none is executed.
Here is the trouble I meet:
Several threads are used in my project and I want to suspend certain threads in some conditions. I use following statements:

::GetExitCodeThread( pThread->hThread, &dwExitCode );
if( dwExitCode == STILL_ACTIVE ){
pThread->SuspendThread();
}
// pThread is the point to the thread

I have to make these statements act as one statement so that the thread will not quit and pThread is valid when "pThread->SuspendThread()" is executed.
Thanks in advance!
Comment
Watch Question

Commented:
Hi!
did you try ctritical sections or mutex (if your threads in different process)?
<<...the thread will not quit and pThread is
valid when "pThread->SuspendThread()" is executed>>

Looks like that pThread becomes invalid when the thread terminated. If so then your code is wrong in principle.

If the thread already exited then you cannot execute even ::GetExitCodeThread( pThread->hThread, &dwExitCode );
CERTIFIED EXPERT
Author of the Year 2009
Commented:
NickRepin,
>>If the thread already exited then you cannot execute even ::GetExitCodeThread( pThread->hThread, &dwExitCode
);

This is not 100% true.  If pThread's m_bAutoDelete has been set to FALSE, then the CWinThread object to which pThread points will continue to exist after it exits.  And its hThead member will continue to be valid for use in a ::GetExitCodeThread() API call.

-==-=-=-=-=-=-=-=-=-=-=-
Lite,
I don't know of a way to say "Don't multitask away from this thread during the following..."

You can boost the priority of the current thread to minimize the chance that control will slip away.... or you can do Sleep(0) directly before the critical code so that you will have a complete timeslice to process the next several instructions...

In any case, I doubt that this is your actual problem (the other thread exiting between the two fn calls).  Are you getting an error return from the pThread->SuspendThread() call?  or has pThread been deleted at the time?  Or... just what is the symptom?

-- Dan
DanRollins,

the original question does not say that pThread points to CWinThread. The questioner believes that pThread becomes invalid once the thread exited.

I meant to say that if this is true, then it makes no sense to call GetExitCodeThread() at all.

Author

Commented:
Thanks, everyone. I'v found an alternative way to bypass this complicated problem. I think the question can be resovled using event and critical section. But I don't want to spend my time on it. :)

Explore More ContentExplore courses, solutions, and other research materials related to this topic.