Solved

CreateThread & WaitForSingleObject

Posted on 2015-02-19
5
453 Views
Last Modified: 2015-02-23
Hi Experts,

I'm creating a thread like this:
DWORD dwThreadID;
m_hMyThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) MyThProc, (LPVOID*) this, 0, &dwThreadID);

Later on, I'm waiting on this thread to finish, with this:
WaitForSingleObject(m_hMyThread, INFINITE);

It seems to be deadlocked.  Is this because this just won't work?  Or is it because something else in my code somewhere is likely deadlocking?

Thanks!
Mike
0
Comment
Question by:thready
  • 2
  • 2
5 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 250 total points
ID: 40619714
That's the usual method of waiting for a thread to terminate. But: Does your thread terminate properly? If it doesn't, the thread that calls 'WaitForSingleObject()' will be stuck there forever. If you need the waiting thread to be soewhat responsive, you could use

while (WAIT_TIMEOUT == WaitForSingleObject(m_hMyThread, 1000)) { // use an interval that suits you

    OutputDebugString("Still waiting...");
}

Open in new window


If the waiting thread has a GUI, you might want to consider using 'MsgWaitForMultipleObjects()' (http://msdn.microsoft.com/en-us/library/aa931008.aspx) instead.
0
 
LVL 34

Assisted Solution

by:sarabande
sarabande earned 250 total points
ID: 40620691
you might think of adding some kind of process-thread communication to the program in order to better control termination:

process:
   - creates a controller object
   - creates thread and passes the controller to the thread

thread:
   - uses the controller to pass status information to the process
   - uses the controller to check for termination by user request
   - it does so at every point where an interruption would be possible
   - it definitively avoids any blocking operation. always using timeouts
   - before return (thread exit) it passes the information to the controller

process:
  - always is responsive for user requests
  - if user requests termination the controller would be used to pass this request to the thread
  - on exit it checks whether the thread is still working by getting the thread status from controller
  - if yes it sends termination request via controller
  - wait for the thread to terminate with timeout
  - if the thread is still running after timeout, kill the thread by calling TerminateThread

note, the last action is a last resort. it should not be necessary if the thread was designed properly.

the wait with timeout could be done as suggested by jkr. if your main thread was running a message loop for to catch user responses you also could use a timer message to handle it. by means of the controller (to which your main thread holds a pointer or reference) you could find out whether the thread has terminated or not.

Sara
0
 
LVL 1

Author Closing Comment

by:thready
ID: 40621266
Thank you Sara, I did do something similar to this- nice answer!

I actually had a silly bug which prompted this question.  But I always get good stuff on here.... :)

Cheers,
Mike
0
 
LVL 34

Expert Comment

by:sarabande
ID: 40622756
thanks Mike.

i saw the following by rereading the question:

(LPVOID*) this

LPVOID already is void*.  so LPVOID* actually is void** (what doesn't make any difference to the call though).

you could omit the cast at all since the compiler always would allow an implicit cast to void* . the compiler even accepted the pointer to pointer as a valid argument for void* without warning.

Sara
0
 
LVL 1

Author Comment

by:thready
ID: 40627426
I just saw your last comment- double awesome.  Thanks!  :-)
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question