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
Solved

CreateThread & WaitForSingleObject

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Title # Comments Views Activity
Header of docx file 17 113
Which Linux flavors will this run on? 6 95
max float value 3 51
How to increment counter variables in ANSI  C (Not C# or C++). 9 17
What my article will show is if you ever had to do processing to a listbox without being able to just select all the items in it. My software Visual Studio 2008 crystal report v11 My issue was I wanted to add crystal report to a form and show…
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 …
The goal of this video is to provide viewers with basic examples to understand and use conditional statements 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.

809 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