Click here to Skip to main content
15,895,794 members

WaitforMultipleObjects doesn't seem to trigger .... always times out

Banged Up Abroad asked:

Open original thread
Assistance would be great.

I am just trying to learn multi-threading and the example below should create 10 threads, set them to sleep for a random period of time, wait for them all to trigger they are finished. The ::WaitForMultipleObjects always returns WAIT_TIMEOUT.

Can someone explain to me why the ::WaitForMultipleObjects doesn't complete after all the threads are finished?

I am sure it is pretty simple for the experts and apologies if it is obvious.

Thanks


void CtestmultithreadView::OnGo()
{
  // TODO: Add your command handler code here
  AlgoParam param;
  HANDLE    startHandles[10];
  HANDLE    finishedHandles[10];
  UINT seed = (unsigned)time( NULL );
  srand( seed );
  
  for( int i=0; i<10; i++ )
  {
    startHandles[i] = ::CreateEvent( NULL, FALSE, FALSE, NULL );
    finishedHandles[i] = ::CreateEvent( NULL, FALSE, FALSE, NULL );
    double r = ((double)rand()/RAND_MAX)*10000.0;
    param.startHandle = startHandles[i];
    param.finishedHandle = finishedHandles[i];
    param.i = i;
    param.interval = (DWORD)r;
		
    CWinThread* wt = AfxBeginThread( AlgoController, &param, 0, 0, CREATE_SUSPENDED );
    wt->m_bAutoDelete = TRUE;
    wt->ResumeThread();
    ::SetEvent( startHandles[i] );
    ::Sleep(100);
  }

  DWORD waitResult;
  ATLTRACE( _T("Waiting for multiple objects\n") );
  waitResult = ::WaitForMultipleObjects( 10, finishedHandles, TRUE, 45000 );

  if( waitResult==WAIT_TIMEOUT )
    ATLTRACE( _T("WAIT_TIMEOUT\n") );
  if( waitResult==WAIT_FAILED )
    ATLTRACE( _T("WAIT_FAILED\n") );
  if( waitResult==WAIT_OBJECT_0 )
    ATLTRACE( _T("WAIT_OBJECT_0\n") );
  if( waitResult==WAIT_ABANDONED_0 )
    ATLTRACE( _T("WAIT_ABANDONED_0\n") );
}



UINT AlgoController( LPVOID pParam )
{
  AlgoParam* param = (AlgoParam*)pParam;
	
  int     threadID = param->i;
  DWORD   interval = param->interval;
	
  ::WaitForSingleObject( param->startHandle, INFINITE );
  ATLTRACE2( _T("Thread id : %d sleeping for %ld\n"), threadID, interval );
  ::Sleep( interval );
  ATLTRACE2( _T("Thread id : %d finished\n"), threadID );
  ::SetEvent( param->finishedHandle );
	
  //	AfxEndThread( 0, FALSE );
  return 0L;
}
Tags: MFC, Threads

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900