跨平台的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程序中多线程共享资源的问题(线程数据共 ...
随机推荐
- 去掉WARN spring.jpa.open-in-view is enabled by default
使用springboot jpa,在运行项目时发现一个WARN WARN 11472 --- [ main] aWebConfiguration$JpaWebMvcConfiguration : sp ...
- 【idea】【sonarlint】指定文件夹扫描
选择文件夹右键->Analyze->Analyze with SonarLint
- windows SDK中的wininet写http客户端
BOOL InternetSetOption( _In_ HINTERNET hInternet, _In_ DWORD dwOption, _In_ LPVOID lpBuffer, _In_ ...
- day24——面向对象三大特性、鸭子类型、类的约束、super的深度解析
day24 面向对象的三大特性 继承.封装.多态 封装:把一堆东西(代码,数据)放到一个地方(空间),并且可以使用 class Student: def __init__(self, name, se ...
- Mysql练习题(1)
表名和字段------------------------------------------------------------------------------–1.学生表Student(s_i ...
- 【LEETCODE】72、分割回文串 III 第1278题
package y2019.Algorithm.dynamicprogramming.hard; /** * @Auther: xiaof * @Date: 2019/12/11 08:59 * @D ...
- mysql连接不释放
环境: 持久层:JPA 数据库连接池:druid 数据库中间件:Mycat 数据库:Mysql 报错: Unable to acquire JDBC Connection 排查步骤: 方法一: 1.d ...
- C++编译器会对没有构造函数的类生成默认构造函数吗?(有必要的时候才生成,要看情况。有反汇编验证)
之前在上C++的课的时候,印象中有那么一句话:如果一个类没有任何构造函数,那么编译器会生成一个默认的构造函数 今天在看<深度探索C++对象模型>的第二章:“构造函数语意学”的时候发现之前听 ...
- 对一次 redis 未授权写入攻击的分析以及学习
前段时间自己使用 redis 开发的时候,搞了一个 docker ,然后直接开放连接没有密码,其实一开始我就知道会被黑产扫到然后给我种马,但是把因为也是测试服务,其实也没怎么上心,于是就放任自由了,结 ...
- 如何大幅提高百度云文件的下载速度?油猴+user-agent-switcher+PanDownload使用教程
最近尝试了提高百度云下载速度的方法,最后效果还不错,提供给大家参考.效果如图: 第一步:下载油猴插件 (截图来自word文档,所以是反的^_^||) 第二步:在油猴中添加新插件 搜索百度网盘 添加插 ...