跨平台的EVENT事件 windows linux(转)
#ifndef _HIK_EVENT_H_
#define _HIK_EVENT_H_ #ifdef _MSC_VER
#include <Windows.h>
#define hik_event_handle HANDLE
#else
#include <pthread.h>
typedef struct
{
bool state;
bool manual_reset;
pthread_mutex_t mutex;
pthread_cond_t cond;
}event_t;
#define event_handle event_t*
#endif //返回值:NULL 出错
event_handle event_create(bool manual_reset, bool init_state); //返回值:0 等到事件,-1出错
int event_wait(event_handle hevent); //返回值:0 等到事件,1 超时,-1出错
int event_timedwait(event_handle hevent, long milliseconds); //返回值:0 成功,-1出错
int event_set(event_handle hevent); //返回值:0 成功,-1出错
int event_reset(event_handle hevent); //返回值:无
void event_destroy(event_handle hevent); #endif ////////////
<pre class="cpp" name="code">#include "event.h"
#ifdef __linux
#include <sys/time.h>
#include <errno.h>
#endif
#include <iostream>
event_handle event_create(bool manual_reset, bool init_state)
{
#ifdef _MSC_VER
HANDLE hevent = CreateEvent(NULL, manual_reset, init_state, NULL);
#else
event_handle hevent = new(std::nothrow) event_t;
if (hevent == NULL)
{
return NULL;
}
hevent->state = init_state;
hevent->manual_reset = manual_reset;
if (pthread_mutex_init(&hevent->mutex, NULL))
{
delete hevent;
return NULL;
}
if (pthread_cond_init(&hevent->cond, NULL))
{
pthread_mutex_destroy(&hevent->mutex);
delete hevent;
return NULL;
}
#endif
return hevent;
}
int event_wait(event_handle hevent)
{
#ifdef _MSC_VER
DWORD ret = WaitForSingleObject(hevent, INFINITE);
if (ret == WAIT_OBJECT_0)
{
return ;
}
return -;
#else
if (pthread_mutex_lock(&hevent->mutex))
{
return -;
}
while (!hevent->state)
{
if (pthread_cond_wait(&hevent->cond, &hevent->mutex))
{
pthread_mutex_unlock(&hevent->mutex);
return -;
}
}
if (!hevent->manual_reset)
{
hevent->state = false;
}
if (pthread_mutex_unlock(&hevent->mutex))
{
return -;
}
return ;
#endif
}
int event_timedwait(event_handle hevent, long milliseconds)
{
#ifdef _MSC_VER
DWORD ret = WaitForSingleObject(hevent, milliseconds);
if (ret == WAIT_OBJECT_0)
{
return ;
}
if (ret == WAIT_TIMEOUT)
{
return ;
}
return -;
#else int rc = ;
struct timespec abstime;
struct timeval tv;
gettimeofday(&tv, NULL);
abstime.tv_sec = tv.tv_sec + milliseconds / ;
abstime.tv_nsec = tv.tv_usec* + (milliseconds % )*;
if (abstime.tv_nsec >= )
{
abstime.tv_nsec -= ;
abstime.tv_sec++;
} if (pthread_mutex_lock(&hevent->mutex) != )
{
return -;
}
while (!hevent->state)
{
if (rc = pthread_cond_timedwait(&hevent->cond, &hevent->mutex, &abstime))
{
if (rc == ETIMEDOUT) break;
pthread_mutex_unlock(&hevent->mutex);
return -;
}
}
if (rc == && !hevent->manual_reset)
{
hevent->state = false;
}
if (pthread_mutex_unlock(&hevent->mutex) != )
{
return -;
}
if (rc == ETIMEDOUT)
{
//timeout return 1
return ;
}
//wait event success return 0
return ;
#endif
}
int event_set(event_handle hevent)
{
#ifdef _MSC_VER
return !SetEvent(hevent);
#else
if (pthread_mutex_lock(&hevent->mutex) != )
{
return -;
} hevent->state = true; if (hevent->manual_reset)
{
if(pthread_cond_broadcast(&hevent->cond))
{
return -;
}
}
else
{
if(pthread_cond_signal(&hevent->cond))
{
return -;
}
} if (pthread_mutex_unlock(&hevent->mutex) != )
{
return -;
} return ;
#endif
}
int event_reset(event_handle hevent)
{
#ifdef _MSC_VER
//ResetEvent 返回非零表示成功
if (ResetEvent(hevent))
{
return ;
}
return -;
#else
if (pthread_mutex_lock(&hevent->mutex) != )
{
return -;
} hevent->state = false; if (pthread_mutex_unlock(&hevent->mutex) != )
{
return -;
}
return ;
#endif
}
void event_destroy(event_handle hevent)
{
#ifdef _MSC_VER
CloseHandle(hevent);
#else
pthread_cond_destroy(&hevent->cond);
pthread_mutex_destroy(&hevent->mutex);
delete hevent;
#endif
}
跨平台的EVENT事件 windows linux(转)的更多相关文章
- 跨平台的EVENT事件 windows linux
#ifndef _HIK_EVENT_H_ #define _HIK_EVENT_H_ #ifdef _MSC_VER #include <Windows.h> #define hik_e ...
- 使用Active Database Duplication创建跨平台Data Guard设置 (Windows/Linux) (Doc ID 881421.1)
Using Active Database Duplication to Create Cross Platform Data Guard Setup (Windows/Linux) (Doc ID ...
- .NET Core 跨平台 串口通讯 ,Windows/Linux 串口通讯,flyfire.CustomSerialPort 的使用
目录 1,前言 2,安装虚拟串口软件 3,新建项目,加入 flyfire.CustomSerialPort 4,flyfire.CustomSerialPort 说明 5,开始使用 flyfire.C ...
- mysql定时任务(event事件)
1.event事件 事件(event)是MySQL在相应的时刻调用的过程式数据库对象.一个事件可调用一次,也可周期性的启动,它由一个特定的线程来管理的,也就是所谓的“事件调度器” 事件和触发器类似,都 ...
- <学习opencv>跨平台和本机windows
/*=========================================================================*/ // 跨平台和本机Windows /*=== ...
- event 事件2
4.事件类型 “DOM3级事件”规定了一下几类事件: 1)UI事件(用户界面事件),当用户与页面元素交互时触发 2)焦点事件,当元素获得或失去焦点时触发 3)鼠标事件,当用户通过鼠标在页面上执行操作时 ...
- IDA Pro Disassembler 6.8.15.413 (Windows, Linux, Mac)
IDA: What's new in 6.8 Highlights This is mainly a maintenance release, so our focus was on fixing b ...
- windows/Linux下设置ASP.Net Core开发环境并部署应用
10分钟学会在windows/Linux下设置ASP.Net Core开发环境并部署应用 创建和开发ASP.NET Core应用可以有二种方式:最简单的方式是通过Visual Studio 2017 ...
- GIL与普通互斥锁区别,死锁现象,信号量,event事件,进程池与线程池,协程
GIL与普通互斥锁区别 GIL锁和互斥锁的异同点 相同: 都是为了解决解释器中多个线程资源竞争的问题 异: 1.互斥锁是Python代码层面的锁,解决Python程序中多线程共享资源的问题(线程数据共 ...
随机推荐
- jQuery正则校验
jQuery正则校验 银行卡号 //验证银行卡号,bankno为银行卡号function luhnCheck(){ var bankno = $.trim($("#bankNoInp&quo ...
- netcore 步骤
1.创建工程目录 d:\project 2.进入目录,创建解决方案 dotnet new sln 3.确定开发版本 dotnet --list-sdks //列出sdk版本 dotnet new gl ...
- zookeeper从入门到精通视频教程(含网盘下载地址)
Zookeeper视频教程链接:https://pan.baidu.com/s/1V9YZN5F3sTKQJOhiDt9hnA 提取码:rtgl
- PHP 输入输出流 php://input 获取表单中2个重名name的值
PHP 输入输出流 php://input 获取表单中2个重名name的值 <?php // PHP有一种"所有IO都是流"的说法. // 压缩流参考 https://w ...
- django 无限层级的评论
一.摘要 拓展 django 官方的评论库,为评论提供无限层级的支持. 二.demo演示 访问链接: https://github.com/zmrenwu/django-mptt-comments 下 ...
- Oracle将小于1的数字to_char后,丢掉小数点前0的解决办法
使用to_char方法将小于0的数字转化为字符串时会出现小数点前0丢失的问题: 解决方案: 使用 oracle的tochar() 函数,并指定位数. --解决方案: 使用 oracle的tochar( ...
- JAVA简易数据连接池Condition
用Condition和synchronized: import java.sql.Connection; import java.sql.DriverManager; import java.sql. ...
- python基础知识(七)---数据类型补充、"雷区"、编码
数据类型补充."雷区".编码 1.数据类型补充 str: #字符串数据类型补充 s1=str(123) #常用于类型转换 print(s1) #capitalize()首字母大写 ...
- permission
import 'package:flutter/material.dart'; import 'dart:io'; import 'dart:async'; import 'package:rxdar ...
- 介绍一种在ABAP内核态进行内表高效拷贝的方法,和对应的Java和JavaScript版本的伪实现
内表操作是ABAP开发人员几乎在每个ABAP程序里都会遇到的. 看一个例子:有两个行结构不一样的内表,每个内表的行结构有三列,除了name这一列名字一致外,其他两列的名称都不同,下图用红色和蓝色标注出 ...