linux_syn.h

来自「2002年」· C头文件 代码 · 共 60 行

H
60
字号
#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 + =
减小字号Ctrl + -
显示快捷键?