- 论坛徽章:
- 0
|
我想请问,在Unix环境下,怎样才能模拟Windows下的WaitForMultipleObjects这个函数,本人是这样实现的:
DWORD WaitForMultipleObjects(
DWORD nCount, // number of handles in array
HANDLE *lpHandles, // object-handle array
BOOL fWaitAll, // wait option
DWORD dwMilliseconds // time-out interval
)
{
int iVal;
BOOL bFlag = TRUE;
int i;
DWORD BlockTime=0;
struct sembuf m_ops[1];
do
{
m_ops[0].sem_op = -1;/*取资源*/
m_ops[0].sem_flg = IPC_NOWAIT;
//m_ops[0].sem_flg = 0;
m_ops[0].sem_num = 0;
for( i = 0; i < nCount; i++ )
{
iVal = semop( lpHandles, m_ops, 1 );
if ( iVal == -1 && errno != EAGAIN && errno != EINTR)
{
printf( "WaitForMultipleObjects error:%s\n", strerror( errno ) );
return WAIT_ABANDONED_0;
}
if( iVal == 0 && !fWaitAll ) /* iVal==0, signal */
{
return i; /*For fWaitAll=FALSE:return Values */
}
if( iVal != 0 )
bFlag = FALSE;
}//end for
Sleep(50);
if( dwMilliseconds == INFINITE )
{
BlockTime = INFINITE;//Block Wait
Sleep(100);
//sleep(1);
}
else
{
BlockTime += 50;
}
} while ( bFlag == FALSE && BlockTime <= dwMilliseconds );
return ((bFlag == TRUE) ? i : WAIT_TIMEOUT);
}
这样实现就会造成死锁,而且不能使CPU真正投入睡眠状态,请高手指点,怎么实现才好?多谢! |
|