跨平台的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 0;
}
return -1;
#else
if (pthread_mutex_lock(&hevent->mutex))
{
return -1;
}
while (!hevent->state)
{
if (pthread_cond_wait(&hevent->cond, &hevent->mutex))
{
pthread_mutex_unlock(&hevent->mutex);
return -1;
}
}
if (!hevent->manual_reset)
{
hevent->state = false;
}
if (pthread_mutex_unlock(&hevent->mutex))
{
return -1;
}
return 0;
#endif
}
int event_timedwait(event_handle hevent, long milliseconds)
{
#ifdef _MSC_VER
DWORD ret = WaitForSingleObject(hevent, milliseconds);
if (ret == WAIT_OBJECT_0)
{
return 0;
}
if (ret == WAIT_TIMEOUT)
{
return 1;
}
return -1;
#else int rc = 0;
struct timespec abstime;
struct timeval tv;
gettimeofday(&tv, NULL);
abstime.tv_sec = tv.tv_sec + milliseconds / 1000;
abstime.tv_nsec = tv.tv_usec*1000 + (milliseconds % 1000)*1000000;
if (abstime.tv_nsec >= 1000000000)
{
abstime.tv_nsec -= 1000000000;
abstime.tv_sec++;
} if (pthread_mutex_lock(&hevent->mutex) != 0)
{
return -1;
}
while (!hevent->state)
{
if (rc = pthread_cond_timedwait(&hevent->cond, &hevent->mutex, &abstime))
{
if (rc == ETIMEDOUT) break;
pthread_mutex_unlock(&hevent->mutex);
return -1;
}
}
if (rc == 0 && !hevent->manual_reset)
{
hevent->state = false;
}
if (pthread_mutex_unlock(&hevent->mutex) != 0)
{
return -1;
}
if (rc == ETIMEDOUT)
{
//timeout return 1
return 1;
}
//wait event success return 0
return 0;
#endif
}
int event_set(event_handle hevent)
{
#ifdef _MSC_VER
return !SetEvent(hevent);
#else
if (pthread_mutex_lock(&hevent->mutex) != 0)
{
return -1;
} hevent->state = true; if (hevent->manual_reset)
{
if(pthread_cond_broadcast(&hevent->cond))
{
return -1;
}
}
else
{
if(pthread_cond_signal(&hevent->cond))
{
return -1;
}
} if (pthread_mutex_unlock(&hevent->mutex) != 0)
{
return -1;
} return 0;
#endif
}
int event_reset(event_handle hevent)
{
#ifdef _MSC_VER
//ResetEvent 返回非零表示成功
if (ResetEvent(hevent))
{
return 0;
}
return -1;
#else
if (pthread_mutex_lock(&hevent->mutex) != 0)
{
return -1;
} hevent->state = false; if (pthread_mutex_unlock(&hevent->mutex) != 0)
{
return -1;
}
return 0;
#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程序中多线程共享资源的问题(线程数据共 ...
随机推荐
- Python数据挖掘-航空公司客户价值分析
出处:http://www.ithao123.cn/content-11127869.html 航空公司客户价值分析 目标:企业针对不同价值的客户制定个性化的服务,将有限的资源集中于高价值客户. 1. ...
- dubbo-admin管理控制台的安装和使用
因为zookeeper只是一个黑框,我们无法看到是否存在了什么提供者或消费者,这时就要借助Dubbo-Admin管理平台来实时的查看,也可以通过这个平台来管理提者和消费者. dubbo下载及配置 du ...
- laravel5.5缓存系统
目录 1 Redis的配置 1.1 安装PRedis 1.2 配置 1.2.1 配置redis数据库 1.2.2 更改session的配置 1.2.3 更改cache配置 1.3 使用redis 2 ...
- php jsonp单引号转义
php中jsonp输出时一般用下面的格式: callbackname('json string'); 如果中间的json string中含有单引号,这个输出就是有问题的,调用方一般是无法处理的,所以我 ...
- 【Theory of Generalization】林轩田机器学习基石
紧接上一讲的Break Point of H.有一个非常intuition的结论,如果break point在k取到了,那么k+1, k+2,... 都是break point. 那么除此之外,我们还 ...
- python负数除法与模运算
1.负数除法: >>> print 45/76>>> print -45/7-7 >>> print 45/-7-7 >>> p ...
- IOS与Unity交互
学习了宣雨松的两篇Unity和IOS交互的文章,自己动手做了下,遇到了些问题,在此记录 先说IOS发送消息给Unity:(文章地址:http://www.xuanyusong.com/archives ...
- RAID介绍和实现
RAID的全称是廉价磁盘冗余阵列(Redundant Array of Inexpensive Disks),于1987年由美国Berkeley 大学的两名工程师提出的. RAID出现的,最初目的是将 ...
- shell文本处理工具总结
shell文本处理工具总结 为了效率,应该熟练的掌握自动化处理相关的知识和技能,能力就表现在做同样的一件事情,可以做的很好的同时,耗时还很短. 再次总结shell文本处理的相关规则,对提高软件调试效率 ...
- linux误删除恢复
extundelete 大家基本都知道,在linux上误删除了东西后果是很严重的,尤其是在服务器上误删除了东西,对于字符终端,想要实现恢复删除的数据更是难上加难,对于Linux误删除了重要的东西,虽然 ...