-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestAtomicLock.cc
58 lines (57 loc) · 2.44 KB
/
TestAtomicLock.cc
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
/*************************************************************************
@Author: jiapeng.wen([email protected])
@Created Time : Wed 11 Sep 2019 02:34:05 PM CST
@File Name: TestAtomicLock.cc
@brief:
************************************************************************/
#include <iostream>
#include <thread>
#include "LockedStackT.h"
#include "SpinLockByTasT.h"
#include "SpinLockByCasT.h"
#include "MemoryReclamationByReference.h"
#include "MemoryReclamationByHazardPointer.h"
#include "LockFreeStack.h"
template<typename _Ty, int _ThreadCount = 16, int _LoopCount = 100000>
struct LockFreePerformanceTestT
{
template<class _ProcessUnit>
static double Run(_ProcessUnit puf)
{
std::thread ths[_ThreadCount];
auto st = std::chrono::high_resolution_clock::now();
for (int i = 0; i < _ThreadCount; ++i) ths[i] = std::thread([&puf]() {
for (int i = 0; i < _LoopCount; ++i) {
puf();
}
});
for (int i = 0; i < _ThreadCount; ++i) ths[i].join();
const double period_in_ms = static_cast<double>((std::chrono::high_resolution_clock::now() - st).count())
/ std::chrono::high_resolution_clock::period::den * 1000;
return period_in_ms;
}
static void Run()
{
_Ty s;
std::cout << Run([&s]() {s.Push(0); }) << "\t\t";
std::cout << Run([&s]() { s.Pop(); }) << std::endl;
}
};
int main()
{
std::cout << "LockedStack with std::mutex" << "\t\t\t\t\t";
LockFreePerformanceTestT<LockedStackT<uint32_t, std::mutex>>::Run();
std::cout << "LockedStack with SpinLockByTas yield" << "\t\t\t\t";
LockFreePerformanceTestT<LockedStackT<uint32_t, SpinLockByTasT<>>>::Run();
std::cout << "LockedStack with SpinLockByCas yield" << "\t\t\t\t";
LockFreePerformanceTestT<LockedStackT<uint32_t, SpinLockByCasT<>>>::Run();
std::cout << "LockedStack with SpinLockByTas usleep(5)" << "\t\t\t";
LockFreePerformanceTestT<LockedStackT<uint32_t, SpinLockByTasT<5>>>::Run();
std::cout << "LockedStack with SpinLockByCas usleep(5)" << "\t\t\t";
LockFreePerformanceTestT<LockedStackT<uint32_t, SpinLockByCasT<5>>>::Run();
std::cout << "LockFreeStack with MemoryReclamationByReferenceCounting" << "\t\t";
LockFreePerformanceTestT<LockFreeStackT<uint32_t, MemoryReclamationByReferenceCountingT<NodeT<uint32_t>>>>::Run();
std::cout << "LockFreeStack with MemoryReclamationByHazardPointer" << "\t\t";
LockFreePerformanceTestT<LockFreeStackT<uint32_t, MemoryReclamationByHazardPointerT<NodeT<uint32_t>>>>::Run();
return 0;
}