Linux 下 Mutex 的简易包装

来源:互联网 发布:华星全站仪传输软件 编辑:程序博客网 时间:2024/05/29 17:38

1.

这是  API_Mutex.hpp

#ifndef_API_MUTEX_HPP_#define_API_MUTEX_HPP_#include <pthread.h>namespace API{//互斥量class API_Mutex{public:API_Mutex();//构造函数virtual ~API_Mutex();//虚析构函数private:pthread_mutex_tm_Mutex;//互斥量public:void Lock();//加锁void UnLock();//解锁};};#endif

2. 这个是 API_Mutex.cpp

#include "API_Mutex.hpp"#include <pthread.h>//构造函数API::API_Mutex::API_Mutex(){pthread_mutexattr_tMutexAttr;memset(&MutexAttr, 0, sizeof(pthread_mutexattr_t));        pthread_mutexattr_init(&MutexAttr);pthread_mutexattr_settype(&MutexAttr, PTHREAD_MUTEX_RECURSIVE_NP);pthread_mutex_init(&m_Mutex, &MutexAttr);}//虚析构函数API::API_Mutex::~API_Mutex(){pthread_mutex_destroy(&m_Mutex);}//加锁void API::API_Mutex::Lock(){pthread_mutex_lock(&m_Mutex);}//解锁void API::API_Mutex::UnLock(){pthread_mutex_unlock(&m_Mutex);}


知识无价。

转载,请注明出处!! 

原创粉丝点击