C++简单封装互斥量

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/KentZhang_/article/details/48180865

在编写多线程程序中,经常需要遇到多线程同步操作,例如操作sqlite3数据库,写一个日志文件,操作一个链表等,只能同时一个线程操作,因此需要经常用到互斥锁。

笔者为了方便使用,采用C++简单封装Linux互斥锁,根据构造函数的参数,可以创建递归互斥锁和非递归互斥锁。


代码如下:

CMutex.h

/*************************************************************************
    > File Name: CMutex.h
    > Author: KentZhang
    > Mail: [email protected]
    > Created Time: Wed 02 Sep 2015 01:49:15 PM CST
 ************************************************************************/

#ifndef __CMUTEX_H__
#define __CMUTEX_H__
#include <pthread.h>
#include <sys/types.h>
class CMutex
{
public:
	CMutex(bool IsRecursive=false);//默认创建非递归互斥锁
	~CMutex();
	bool Lock();
	bool UnLock();
	bool TryLock();
private:
	pthread_mutex_t *m_pMutex;
};

#endif

CMutex.cpp

/*************************************************************************
  > File Name: CMutex.cpp
  > Author: KentZhang
  > Mail: [email protected]
  > Created Time: Wed 02 Sep 2015 01:46:36 PM CST
 ************************************************************************/

//封装互斥锁
#include "CMutex.h"
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define DEBUG_ERROR(format,...)  do{ printf(""format", FileName:%s, FuncName:%s, LineNum:%d\n",\
				     ##__VA_ARGS__, __FILE__, __func__, __LINE__);}while(0)

CMutex::CMutex(bool IsRecursive)
{
	m_pMutex = new pthread_mutex_t;
	pthread_mutexattr_t attr;
	pthread_mutexattr_init(&attr);
	int nResult = 0;
	do{
		if (IsRecursive)
		{  
			//设置属性为可递归
			nResult = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
			nResult = 20;
			if ( 0 != nResult )
			{
				DEBUG_ERROR("pthread_mutexattr_settype() failed:%s", strerror(nResult));
				break;
			}
		}
		else
		{   
			//设置属性为不可递归
			nResult = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL);
			if ( 0 != nResult )
			{
				DEBUG_ERROR("pthread_mutexattr_settype() failed:%s", strerror(nResult));
				break;
			}	
		}

		nResult = pthread_mutex_init(m_pMutex, &attr);
		if( 0 != nResult )
		{
			DEBUG_ERROR("pthread_mutexattr_settype() failed:%s", strerror(nResult));
			break;
		}
	}while(0);
	
}

CMutex::~CMutex()
{
	int nResult = pthread_mutex_destroy(m_pMutex);
	if( nResult != 0 )
	{
		DEBUG_ERROR("CMutex ~CMutex() pthread_mutex_destroy() failed:%s", strerror(nResult));
	}
	if ( m_pMutex != NULL )
		delete m_pMutex;
}

bool CMutex::Lock()
{
	int nResult = pthread_mutex_lock(m_pMutex);
	if( nResult < 0 )
	{
		DEBUG_ERROR("CMutex lock() pthread_mutex_lock() failed:%s", strerror(nResult));
		return false;
	}
	return true;
}

bool CMutex::UnLock()
{
	int nResult = pthread_mutex_unlock(m_pMutex);
	if ( nResult < 0 )
	{
		DEBUG_ERROR("CMutex unlock() pthread_mutex_unlock() failed:%s", strerror(nResult));
		return false;
	}
	return true;
}

bool CMutex::TryLock()
{
	int nResult = pthread_mutex_trylock(m_pMutex);
	if ( nResult !=  0 )
	{
		DEBUG_ERROR("CMutex trylock() pthread_mutex_trylock() failed:%s", strerror(nResult));
		return false;
	}
	return true;
}


由于笔者的水平有限,出错在所难免,恳请读者拍砖指正,谢谢阅读

猜你喜欢

转载自blog.csdn.net/KentZhang_/article/details/48180865