Solved

Thread nerver returns from WaitForSingleObject() !!!

Posted on 2000-03-22
2
237 Views
Last Modified: 2013-12-03
I've a Mutex and 3 threads like below

HANDLE HMutex;  
bool   End = false;
//////
/////
void Thread_Running_1()
{
  do
  {
     WaitForSingleObejct( HMutex, INFINITE );
     // ..... do something important
  }
  while( ! End );
}  
//////
/////
void Thread_Running_2()
{
  do
  {
     WaitForSingleObejct( HMutex, INFINITE );
     // ..... do something important
  }
  while( ! End );
}  
//////
//////   The main() function is only a case school
//////
void main()
{
   // I create and lock the mutex

   HMutex = CreateMutex( ..... );
   WaitForSingleObejct( HMutex, INFINITE );

   // I create 2 threads and i start these threads

   CreateThread( ...., Thread_Running_1, ..... );
   CreateThread( ...., Thread_Running_2, ..... );

   // At this time, Thread 1 and 2 are in a wait state.
   // Now, We release and close the Mutex

   End = true;
   ReleaseMutex( HMutex );
   CloseHandle( HMutex );
   
   // At this time, the HMutex is REALLY DESTROYED
}  


The trouble is when i release and close the Mutex handle,
only one thread will continue running. The other thread is
DEFINITELY lost. You can see the thread in "perfmon.exe".
You must call TerminateThread() api to shutdown the thread,
because the thread never returns from WaitForSingleObject().

When i close the Mutex handle, i do not know how many threads
are awaiting the mutex to be released. We could think that
the CloseHandle() will cease the WaitForSingleObject() execution
by returning WAIT_FAILED to threads but it DOES NOT.

This is same thing for Critical Section, Event, Semaphore.

MY QUESTION :
How to prevent the thread from being lost ?
 
0
Comment
Question by:mars
  • 2
2 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 100 total points
ID: 2646034
First of all, you really shoudl be using critical sections for this, not mutexes.

next you need to declare end as volatile, otherwise it may be optimized out of existance, preventing the threads from ending.  (VERY likely.)

Finally  you should not close the mutex handle unil the other threads finish.  To do this you can wait on the thread handles with WaitForSingleObject().  The wait function will return when the thread ends.  So use this to wait until two the additional threads are done, then close the mutex handle

let me know if you have any quesitons.
0
 
LVL 22

Expert Comment

by:nietod
ID: 2646046
One more thing the threads that lock the mutex never release it.  You should have them release the mutex before then terminate.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Windows Standards for file storage. 3 26
Issue when running a SSIS package (.dts) from an SQL job 21 83
Clean Uninstall of VS Professional 2015 5 52
Using MS Code on my Mac 6 68
This article surveys and compares options for encoding and decoding base64 data.  It includes source code in C++ as well as examples of how to use standard Windows API functions for these tasks. We'll look at the algorithms — how encoding and decodi…
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…
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…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

832 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