linux和windows实现事件触发的通信方式

-----------------------  linux -----------------------    
//初始化
pthread_mutex_t mutex_lock;  
pthread_cond_t handle:
pthread_cond_init(&handle, NULL);
  
//等待事件触发
struct timeval now_time;
gettimeofday(&now_time, NULL);
struct timespec abstime;
struct timeval now;
gettimeofday(&now, NULL);
unsigned int nsec = now.tv_usec * 1000 + (time_out % 1000) * 1000000;
abstime.tv_nsec = nsec % 1000000000;
abstime.tv_sec = now.tv_sec + nsec/1000000000 + time_out/1000;


int iRet = pthread_cond_timedwait(&handle, &mutex_lock, &abstime);
if(iRet != 0)
{
    printf("Time Out !!!\n");
    return 0 ;
}
        
//通知,事件触发
pthread_cond_signal(&handle);    

//释放  
pthread_cond_destroy(&handle);    

    


-----------------------  windows -----------------------  
//初始化
HANDLE  handle;
handle = CreateEvent(NULL,FALSE,FALSE,NULL);
if(handle == NULL)
{
    printf("CreateEvent failed (%d)\n", GetLastError());
}

//等待事件触发
int time_out = 1000;
DWORD iRet = WaitForSingleObject(handle, time_out);
if(iRet == WAIT_TIMEOUT || wait_obj == iRet)
{
    //超时
    return 0;
}

//通知,事件触发
if(!SetEvent(handle))
{
    printf("SetEvent failed (%d)\n", GetLastError());
    return -1;
}

//释放  
if(handle != NULL)
{
    CloseHandle(handle);

}  


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值