-
Notifications
You must be signed in to change notification settings - Fork 0
/
spinlock_test.cpp
63 lines (47 loc) · 1.23 KB
/
spinlock_test.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
#include "orderbook.h"
#define BOOST_TEST_MODULE spinlock
#include <boost/test/included/unit_test.hpp>
#include "test.h"
BOOST_AUTO_TEST_CASE( spinlock_basic ) {
SpinLock lock;
{
auto guard = lock.lock();
BOOST_TEST(lock.try_lock()==false);
BOOST_TEST(lock.is_locked()==true);
}
BOOST_TEST(lock.is_locked()==false);
BOOST_TEST(lock.try_lock()==true);
lock.unlock();
BOOST_TEST(lock.try_lock()==true);
}
BOOST_AUTO_TEST_CASE( spinlock_lambda) {
SpinLock lock;
auto guard =
[&] {
auto guard = lock.lock();
return guard;
} ();
BOOST_TEST(lock.is_locked()==true);
}
BOOST_AUTO_TEST_CASE( spinlock_multithread) {
SpinLock lock;
std::vector<std::thread> threads;
long count = 0;
{
auto guard = lock.lock();
auto fn = [&](){
for(int i=0;i<1000000;i++) {
{
auto guard = lock.lock();
count++;
}
}
};
threads.push_back(std::thread(fn));
threads.push_back(std::thread(fn));
}
for(auto thread = threads.begin();thread!=threads.end();thread++) {
thread->join();
}
BOOST_TEST(count==2000000);
}