forked from wyh267/Cplusplus_Thread_Lib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CLinuxMutex.cpp
executable file
·85 lines (45 loc) · 1.24 KB
/
CLinuxMutex.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
#include "CLinuxMutex.h"
CLinuxMutex::CLinuxMutex(const char *pName):
CMutex(pName)
{
m_thread = (pthread_t) 0;
pthread_mutexattr_init(&m_mtex_attr);
if (pthread_mutexattr_settype(&m_mtex_attr, PTHREAD_MUTEX_ERRORCHECK_NP))
printf("CLinuxMutex::CnlLinuxMutex() : Failed to set mutex attibutes\n");
pthread_mutex_init(&m_mutex, &m_mtex_attr);
}
CLinuxMutex::~CLinuxMutex()
{
}
bool CLinuxMutex::Lock()
{
bool locked = false;
int result;
result = pthread_mutex_lock(&m_mutex);
switch (result) {
case 0:
m_thread = pthread_self();
locked = true;
break;
case EDEADLK:
locked = true;
break;
default:
break;
}
return locked;
}
bool CLinuxMutex::UnLock()
{
int result;
bool unlocked = true;
if (m_thread!= pthread_self()) {
unlocked = false;
printf("CnlLinuxMutex::Unlock() : Error - Mutex not locked or not owned by the thread! \n");
} else {
result = pthread_mutex_unlock(&m_mutex);
if(result<0)
unlocked=false;
}
return unlocked;
}