-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdlk_simple.cpp
More file actions
96 lines (74 loc) · 2.62 KB
/
dlk_simple.cpp
File metadata and controls
96 lines (74 loc) · 2.62 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
template<class value_tt>
class thread_local_helper final {
private:
static inline thread_local value_tt _value{};
public:
thread_local_helper() = default;
~thread_local_helper() = default;
static void set(value_tt &&v) { _value = std::move(v); }
static void set(const value_tt &v) { _value = v; }
static value_tt get() { return _value; }
};
class dlk_helper final {
sw::redis::Redis _redis;
public:
dlk_helper(std::string_view uri) : _redis(sw::redis::Redis(uri.data())) {}
inline bool lock(std::string_view key_sv) {
static constexpr std::chrono::milliseconds ttl{5000};
const auto uuid = util::uuid_snowflake::generator::inst_mt().nextid();
const auto uuid_string = std::to_string(uuid);
thread_local_helper<std::string>::set(uuid_string);
return _redis.set(key_sv, uuid_string, ttl,
sw::redis::UpdateType::NOT_EXIST);
}
inline bool unlock(std::string_view key_sv) {
auto value = thread_local_helper<std::string>::get();
std::string_view script =
"if redis.call('get', KEYS[1]) == ARGV[1] then return "
"redis.call('del', KEYS[1]) else return 0 end";
const auto result = _redis.eval<long long>(
script, std::initializer_list<std::string_view>{key_sv},
std::initializer_list<std::string_view>{value});
return 1 == result;
}
};
int main() {
std::string_view dlk_key = "123";
std::pair<int, int> count;
std::thread t1([dlk_key, &count] {
dlk_helper dlk(
"redis://"
"123456@127.0.0.1:56379?socket_timeout=1s&connect_timeout=1s&keep_alive=true");
for (int i = 0; i < 1000; ++i) {
while (dlk.lock(dlk_key) == false)
std::this_thread::sleep_for(1ms);
count.first += 1;
std::cout << "thread 1 locked...." << util::datetime::current_timestamp()
<< " count: " << count.first << ":" << count.second
<< std::endl;
// std::this_thread::sleep_for(10ms);
dlk.unlock(dlk_key);
}
});
std::thread t2([dlk_key, &count] {
dlk_helper dlk(
"redis://"
"123456@127.0.0.1:56379?socket_timeout=1s&connect_timeout=1s&keep_alive=true");
for (int i = 0; i < 1000; ++i) {
while (dlk.lock(dlk_key) == false)
std::this_thread::sleep_for(1ms);
count.second += 1;
std::cout << "thread 2 locked...." << util::datetime::current_timestamp()
<< " count: " << count.first << ":" << count.second
<< std::endl;
// std::this_thread::sleep_for(10ms);
dlk.unlock(dlk_key);
}
});
t1.detach();
t2.detach();
while (true) {
std::this_thread::sleep_for(1s);
}
return 0;
}