跨平台的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程序中多线程共享资源的问题(线程数据共 ...
随机推荐
- 【Spring Cloud学习之一】微服务架构
一.网站架构模式发展 单体应用-->SOA-->微服务 1.分布式项目与项目集群分布式项目:根据业务需求进行拆分成N个子系统,多个子系统相互协作才能完成业务流程子系统之间通讯使用RPC远程 ...
- UE4 常用数学
转自:https://dawnarc.com/2016/07/mathlinear-algebra%E5%90%91%E9%87%8F%E7%A7%AF%E5%A4%96%E7%A7%AF%E5%8F ...
- 【C/C++开发】C++之enum枚举量声明、定义、使用与枚举类详解与枚举类前置类型声明
众所周知,C/C++语言可以使用#define和const创建符号常量,而使用enum工具不仅能够创建符号常量,还能定义新的数据类型,但是必须按照一定的规则进行,下面我们一起看下enum的使用方法. ...
- visual studio ------- 更改字体和背景颜色
1.打开vs 点击工具 选择选项 2.想要更换主题的也可以更换主题, 3.更改字体 4.更改为护眼小背景 参数为 85 123 205 ee
- [转帖]亚马逊发布自主64核心ARM处理器:单核性能远超铂金至强
亚马逊发布自主64核心ARM处理器:单核性能远超铂金至强 https://news.mydrivers.com/1/660/660383.htm 不知道真假 看样子比华为的鲲鹏920 要牛B . 亚马 ...
- Java设计RestfulApi接口,实现统一格式返回
创建返回状态码枚举 package com.sunny.tool.api.enums; /** * @Author sunt * @Description 响应枚举状态码 * @Date 2019/1 ...
- 2019 C语言测试
求一元二次方程ax² + bx + c = 0的解 a,b,c为任意整数.(10分) 编写一个口令输入程序,让用户不停输入口令,直到输对为止,假设口令为456.(8分) 输出1000-9999之间能 ...
- docker stack 部署 mssql
=============================================== 2019/12/8_第1次修改 ccb_warlock == ...
- Java爬虫https网页内容报错SSLHandshakeException信任(忽略)所有SSL证书
javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building f ...
- AOP & 拦截器
https://www.cnblogs.com/boywwj/p/7502185.html spring aop中@after-returning和@after,@afterThrowing,@Aro ...