forked from andreimaximov/uthread
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmutex.cpp
More file actions
46 lines (34 loc) · 690 Bytes
/
Copy pathmutex.cpp
File metadata and controls
46 lines (34 loc) · 690 Bytes
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
#include <uthread/gtest.hpp>
#include <uthread/uthread.hpp>
namespace uthread {
TEST(MutexTest, AcquireWithoutContension) {
int x = 0;
Mutex mutex;
Executor exe;
exe.add([&]() {
Lock guard(&mutex);
x = 1;
});
exe.run();
ASSERT_EQ(x, 1);
}
TEST(MutexTest, AcquireWithContension) {
int x = 0;
Mutex mutex;
Executor exe;
for (int i = 0; i < 100; i++) {
exe.add([&]() {
Lock guard(&mutex);
x++;
auto copy = x;
// Let's make sure the lock keeps other threads away...
for (int j = 0; j < 100; j++) {
Executor::get()->yield();
ASSERT_EQ(x, copy);
}
});
}
exe.run();
ASSERT_EQ(x, 100);
}
}