Solved

CreateThread & WaitForSingleObject

Posted on 2015-02-19
5
316 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
Comment Utility
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 32

Assisted Solution

by:sarabande
sarabande earned 250 total points
Comment Utility
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
Comment Utility
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 32

Expert Comment

by:sarabande
Comment Utility
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
Comment Utility
I just saw your last comment- double awesome.  Thanks!  :-)
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
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 goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now