⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 linux_syn.h

📁 2002年
💻 H
字号:
#ifndef _LINUX_SYN_H
#define _LINUX_SYN_H

#ifndef WIN32

#include "pthread.h"

class Event{
private:
	pthread_mutex_t	m;
	pthread_cond_t	c;
	bool cond;
public:
	Event(){
		cond = false;
	}
	void init(){
		pthread_mutex_init(&m, NULL);
		pthread_cond_init(&c, NULL);
	}
	void destroy(){
		pthread_cond_destroy(&c);
		pthread_mutex_destroy(&m);
	}
	void SetEvent(){
		pthread_mutex_lock(&m);
		cond = true;
		pthread_mutex_unlock(&m);
		pthread_cond_signal(&c);
	}
	void ResetEvent(){
		pthread_mutex_lock(&m);
		cond = false;
		pthread_mutex_unlock(&m);
	}
	int WaitEvent(const struct timespec *abstime = NULL){
		int rt = 0;
		pthread_mutex_lock(&m);		
		if(!cond){
			if(abstime == NULL){
				rt = pthread_cond_wait(&c, &m);
			}else{
				rt = pthread_cond_timedwait(&c, &m, abstime);
			}
		}
		pthread_mutex_unlock(&m);
		return rt;
	}
};

template<class T> void SetEvent(T& e){
	e.SetEvent();
}
template<class T> void ResetEvent(T& e){
  e.ResetEvent();
}

#endif
#endif //_LINUX_SYN_H

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -