WaitforMultipleObjects似乎不触发....总是超时 [英] WaitforMultipleObjects doesn't seem to trigger .... always times out

查看:142
本文介绍了WaitforMultipleObjects似乎不触发....总是超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

帮助会很棒.

我只是想学习多线程,下面的示例应该创建10个线程,将它们设置为随机睡眠一段时间,等待它们全部触发它们完成. :: WaitForMultipleObjects始终返回WAIT_TIMEOUT.

有人可以向我解释为什么:: WaitForMultipleObjects在所有线程完成后仍未完成吗?

如果可以肯定的话,我相信对于专家和歉意来说,这非常简单.

谢谢


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;
}

推荐答案

Banged Up Abroad写道:
Banged Up Abroad wrote:

AlgoParam param;

//剪...

param.startHandle = startHandles [i];
param.finishedHandle = finishHandles [i];
param.i = i;
param.interval =(DWORD)r;

CWinThread * wt = AfxBeginThread(AlgoController,& param,0,0,CREATE_SUSPENDED);

AlgoParam param;

// snip ...

param.startHandle = startHandles[i];
param.finishedHandle = finishedHandles[i];
param.i = i;
param.interval = (DWORD)r;

CWinThread* wt = AfxBeginThread( AlgoController, &param, 0, 0, CREATE_SUSPENDED );


可能与对发送到每个线程的同一个param对象的引用有关?当它们到达函数末尾时,它们都将设置相同的事件.

如果您为每个线程函数的参数创建了一个新对象,则此问题可能会得到解决.或者,您可以只在线程句柄上单击WaitForMultipleObjects,而不是为此显式创建新事件.


Might it have something to do with a reference to the same param object being sent to each thread? They''d all be setting the same event when they get to the end of the function.

This might be fixed if you created a new object for each thread function''s parameter. Alternatively you could just WaitForMultipleObjects on the thread handles instead of explicitly creating new events for that.


这篇关于WaitforMultipleObjects似乎不触发....总是超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆