?
Solved

CreateThread & WaitForSingleObject

Posted on 2015-02-19
5
Medium Priority
?
582 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 1000 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 35

Assisted Solution

by:sarabande
sarabande earned 1000 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 35

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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 …
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

771 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