C++简单封装互斥量

原文链接:C++简单封装互斥量
发布时间:2015-09-02 17:13:12

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

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

代码如下:

CMutex.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/*************************************************************************
> File Name: CMutex.h
> Author: KentZhang
> Mail: zhchk2010@qq.com
> 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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*************************************************************************
> File Name: CMutex.cpp
> Author: KentZhang
> Mail: zhchk2010@qq.com
> 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;
}

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