-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.cpp
More file actions
112 lines (91 loc) · 3.15 KB
/
test.cpp
File metadata and controls
112 lines (91 loc) · 3.15 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/**
* Implementation of Naive and Lock-free ring-buffer queues and
* performance and verification tests.
*
* Build with (g++ version must be >= 4.5.0):
* $ g++ -Wall -std=c++0x -Wl,--no-as-needed -O2 -D DCACHE1_LINESIZE=`getconf LEVEL1_DCACHE_LINESIZE` lockfree_rb_q.cc -lpthread
*
* I verified the program with g++ 4.5.3, 4.6.1, 4.6.3 and 4.8.1.
*
* Use -std=c++11 instead of -std=c++0x for g++ 4.8.
*
* Copyright (C) 2012-2013 Alexander Krizhanovsky (ak@natsys-lab.com).
*
* This file is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation; either version 3, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
* See http://www.gnu.org/licenses/lgpl.html .
*/
#include <iostream>
#include <atomic>
#include <string>
#include <thread>
#include <vector>
#include <queue>
#include <chrono>
#include "lockless_queue.h"
threading::lockless_queue<std::string>* g_lockless;
void writeSomething(std::string what, int l_itemCount)
{
for (; l_itemCount > 0; --l_itemCount)
{
g_lockless->produce(std::move(what));
}
}
void getSomething(std::atomic<bool>& running)
{
std::shared_ptr<std::string> result;
do
{
result = g_lockless->consume(true);
} while (running || result);
}
int main()
{
int l_minThreadCount, l_maxThreadCount;
int l_minItemCount, l_maxItemCount, l_itemCountIncrement;
std::cout << "Minimum number of threads?:";
std::cin >> l_minThreadCount;
std::cout << "Minimum number of threads?:";
std::cin >> l_maxThreadCount;
std::cout << "Minimum number of items per thread?:";
std::cin >> l_minItemCount;
std::cout << "Maximum number of items per thread?:";
std::cin >> l_maxItemCount;
std::cout << "Item count increment?:";
std::cin >> l_itemCountIncrement;
std::queue < std::thread> l_threadList;
using namespace std::chrono;
system_clock::time_point l_iCntStart;
milliseconds l_updateDuration;
for (int l_curItemCount = l_minItemCount; l_curItemCount <= l_maxItemCount; l_curItemCount += l_itemCountIncrement)
{
for (int l_curThreadCount = l_minThreadCount; l_curThreadCount <= l_maxThreadCount; ++l_curThreadCount)
{
g_lockless = new threading::lockless_queue<std::string>();
std::atomic<bool> l_running = true;
std::thread l_reader1(getSomething, std::ref(l_running));
std::thread l_reader2(getSomething, std::ref(l_running));
l_iCntStart = system_clock::now();
l_threadList.push(std::thread(writeSomething, "This is a test string:)", l_curItemCount));
while (l_threadList.size() > 0)
{
l_threadList.front().join();
l_threadList.pop();
}
l_running = false;
delete g_lockless;
g_lockless = nullptr;
l_reader1.join();
l_reader2.join();
l_updateDuration = duration_cast<milliseconds>(system_clock::now() - l_iCntStart);
std::cout << l_curThreadCount << " Threads " << l_curItemCount << " Items " << " Took " << l_updateDuration.count() << std::endl;
}
}
}