-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_rwlock.cpp
More file actions
50 lines (46 loc) · 1.24 KB
/
Copy pathtest_rwlock.cpp
File metadata and controls
50 lines (46 loc) · 1.24 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
#include "rwlock.hpp"
#include "gtest/gtest.h"
#include <thread>
#include <vector>
#include <chrono>
using namespace std::chrono_literals;
TEST(RWLockTest, test_read) {
lru::RWLock rwlock;
int num = 0;
std::vector<std::thread> ths;
int id = 0;
// 5 writers
for (int i = 0; i < 5; i++) {
ths.emplace_back([&]() mutable {
std::this_thread::sleep_for(200ms * i);
rwlock.writer_lock();
printf("(writer)id = %d, num = %d\n", ++id, ++num);
rwlock.writer_unlock();
});
}
// 10 readers
for (int i = 0; i < 10; i++) {
ths.emplace_back([&]() mutable {
std::this_thread::sleep_for(100ms * i);
rwlock.reader_lock();
printf("(reader)id = %d, num = %d\n", ++id, num);
rwlock.reader_unlock();
});
}
std::for_each(ths.begin(), ths.end(), [](std::thread& th) {
th.join();
});
}
TEST(RWLockTest, basic) {
lru::RWLock rwlock;
rwlock.reader_lock();
rwlock.reader_unlock();
rwlock.writer_lock();
rwlock.writer_unlock();
rwlock.reader_lock();
rwlock.reader_unlock();
}
int main(int argc, char** argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}