-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathspinlock_test.cpp
More file actions
51 lines (38 loc) · 1.02 KB
/
spinlock_test.cpp
File metadata and controls
51 lines (38 loc) · 1.02 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
#define BOOST_TEST_MODULE spinlock
#include <boost/test/included/unit_test.hpp>
#include <thread>
#include "spinlock.h"
BOOST_AUTO_TEST_CASE( spinlock_basic ) {
SpinLock lock;
{
Guard guard(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_multithread) {
SpinLock lock;
std::vector<std::thread> threads;
long count = 0;
{
Guard guard(lock);
auto fn = [&](){
for(int i=0;i<1000000;i++) {
{
Guard guard(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);
}