-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmutex_test.cpp
More file actions
141 lines (123 loc) · 3.69 KB
/
mutex_test.cpp
File metadata and controls
141 lines (123 loc) · 3.69 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/**
*@brief : linux api下的各种锁
*/
#include <pthread.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <format>
#include <time.h>
#include <sstream>
#include <map>
#define LOG(format, ...) \
do \
{ \
char timestr[26] = {0}; \
time_t now = time(NULL); \
tm *time_info = localtime(&now); \
strftime(timestr, sizeof(timestr), "%Y-%m-%D %H:%M:%S", time_info); \
printf("[%s][%d] " format "\n", timestr, pthread_self(), ##__VA_ARGS__); \
} while (0)
// 读写锁
class RwLock
{
public:
/**
* @param if_unlock_thread_exit if unlock the mutex when thread exit
* @param if_process_shared if shared the rwlock between with other process
* @note this init func will throw exception when init error
*/
RwLock(bool if_unlock_thread_exit = false, bool if_process_shared = false) : _mutex(PTHREAD_MUTEX_INITIALIZER)
{
pthread_rwlockattr_t attr;
pthread_rwlockattr_init(&attr);
pthread_rwlockattr_setpshared(&attr, if_process_shared ? PTHREAD_PROCESS_SHARED : PTHREAD_PROCESS_PRIVATE);
int ret = pthread_rwlock_init(&_mutex, &attr);
if (ret != 0)
{
std::stringstream ss;
ss << "pthread_rwlock_init error,code:" << ret;
throw std::runtime_error(ss.str());
}
}
// get read lcok if success return 0
int rlock()
{
return pthread_rwlock_rdlock(&_mutex);
}
// try to get read lock if success return 0
int try_rlock()
{
return pthread_rwlock_tryrdlock(&_mutex);
}
// try to get write lock success return 0
int try_wlock()
{
return pthread_rwlock_trywrlock(&_mutex);
}
// unlock if success return 0
int unlock()
{
return pthread_rwlock_unlock(&_mutex);
}
// get write lock if success return 0
int wlock()
{
return pthread_rwlock_wrlock(&_mutex);
}
~RwLock()
{
pthread_rwlock_destroy(&_mutex);
}
private:
pthread_rwlock_t _mutex;
};
class Mutex
{
public:
/**
* @param if_unlock_thread_exit if unlock the mutex when thread exit
* @param if_process_shared if shared the mutex between with other process
* @note this init func will throw exception when init error
*/
Mutex(bool if_unlock_thread_exit = false, bool if_process_shared = false)
: _mutex(PTHREAD_MUTEX_INITIALIZER)
{
pthread_mutexattr_t attr;
pthread_mutexattr_init(&attr);
pthread_mutexattr_setpshared(&attr, if_process_shared ? PTHREAD_PROCESS_SHARED : PTHREAD_PROCESS_PRIVATE);
int ret = pthread_mutex_init(&_mutex, &attr);
if (ret != 0)
{
std::stringstream ss;
ss << "pthread_mutex_init error, code:" << ret;
throw std::runtime_error(ss.str());
}
}
// lock the mutex, block if not available
int lock()
{
return pthread_mutex_lock(&_mutex);
}
// try to lock the mutex, return 0 if success
int try_lock()
{
return pthread_mutex_trylock(&_mutex);
}
// unlock the mutex
int unlock()
{
return pthread_mutex_unlock(&_mutex);
}
~Mutex()
{
pthread_mutex_destroy(&_mutex);
}
private:
pthread_mutex_t _mutex;
};
std::map<int, std::string> _gloablMap;
int main(int argc, char **argv)
{
return 0;
}