Solved

CreateThread & WaitForSingleObject

Posted on 2015-02-19
5
378 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 33

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 33

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

803 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