mutex.cpp

来自「linux下的线程池,其中使用了条件变量,互斥锁等保持线程同步的变量!」· C++ 代码 · 共 62 行

CPP
62
字号
#include "mutex.h"

Mutex::Mutex()
{
	
#ifndef WIN32
	pthread_mutex_init(&mutex_,NULL);
#endif
}

Mutex::~Mutex()
{
#ifndef WIN32
	pthread_mutex_destroy(&mutex_);
#endif
	
}

bool Mutex::Lock()
{
#ifndef WIN32
	return pthread_mutex_lock(&mutex_) == 0;
#else
	return true;
#endif
	
}

bool Mutex::Unlock()
{
#ifndef WIN32
	return pthread_mutex_unlock(&mutex_) == 0;
#else
	return true;
#endif
	
}

#ifndef WIN32
Mutex::operator pthread_mutex_t&()
{
	return mutex_;

}
#endif


MutexApply::MutexApply(Mutex& mutex)
:mutex_(mutex)
{

	mutex_.Lock();

}


MutexApply::~MutexApply()
{

	mutex_.Unlock();
}

⌨️ 快捷键说明

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