全部博文(1493)
分类:
2012-07-05 08:50:46
原文地址:实用的mutex实现类 作者:aoyang888
01 | #include "mutex.h" |
02 |
03 | namespace ZFPT |
04 | { |
05 | CMutex::CMutex() |
06 | { |
07 | pthread_mutex_init(&m_Mutex, NULL); |
08 | } |
09 |
10 | CMutex::~CMutex() |
11 | { |
12 | pthread_mutex_destroy(&m_Mutex); |
13 | } |
14 |
15 | int CMutex::lock() |
16 | { |
17 | return pthread_mutex_lock(&m_Mutex); |
18 | } |
19 |
20 | int CMutex::tryLock() |
21 | { |
22 | return pthread_mutex_trylock(&m_Mutex); |
23 | } |
24 |
25 | int CMutex::unLock() |
26 | { |
27 | return pthread_mutex_unlock(&m_Mutex); |
28 | } |
29 |
30 | CScopeLock::CScopeLock(CMutex& cMutex, bool IsTry): m_Mutex(cMutex) |
31 | { |
32 | if(IsTry) |
33 | { |
34 | m_Mutex.tryLock(); |
35 | } |
36 | else |
37 | { |
38 | m_Mutex.lock(); |
39 | } |
40 | } |
41 |
42 | CScopeLock::~CScopeLock() |
43 | { |
44 | m_Mutex.unLock(); |
45 | } |
46 |
47 |
48 |
49 | } |