3399 |
30 Mar 15 |
peter |
// $Id$ |
3399 |
30 Mar 15 |
peter |
2 |
|
3399 |
30 Mar 15 |
peter |
3 |
/* |
3999 |
08 Oct 20 |
peter |
Copyright (C) 2015, 2019, 2020 Peter Johansson |
3399 |
30 Mar 15 |
peter |
5 |
|
3399 |
30 Mar 15 |
peter |
This file is part of the yat library, http://dev.thep.lu.se/yat |
3399 |
30 Mar 15 |
peter |
7 |
|
3399 |
30 Mar 15 |
peter |
The yat library is free software; you can redistribute it and/or |
3399 |
30 Mar 15 |
peter |
modify it under the terms of the GNU General Public License as |
3399 |
30 Mar 15 |
peter |
published by the Free Software Foundation; either version 3 of the |
3399 |
30 Mar 15 |
peter |
License, or (at your option) any later version. |
3399 |
30 Mar 15 |
peter |
12 |
|
3399 |
30 Mar 15 |
peter |
The yat library is distributed in the hope that it will be useful, |
3399 |
30 Mar 15 |
peter |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
3399 |
30 Mar 15 |
peter |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
3399 |
30 Mar 15 |
peter |
General Public License for more details. |
3399 |
30 Mar 15 |
peter |
17 |
|
3399 |
30 Mar 15 |
peter |
You should have received a copy of the GNU General Public License |
3399 |
30 Mar 15 |
peter |
along with yat. If not, see <http://www.gnu.org/licenses/>. |
3399 |
30 Mar 15 |
peter |
20 |
*/ |
3399 |
30 Mar 15 |
peter |
21 |
|
3399 |
30 Mar 15 |
peter |
22 |
#include <config.h> |
3399 |
30 Mar 15 |
peter |
23 |
|
3399 |
30 Mar 15 |
peter |
24 |
#include "yat/utility/PriorityQueue.h" |
3399 |
30 Mar 15 |
peter |
25 |
|
3399 |
30 Mar 15 |
peter |
26 |
#include "Suite.h" |
3399 |
30 Mar 15 |
peter |
27 |
|
3399 |
30 Mar 15 |
peter |
28 |
#include <iostream> |
3948 |
20 Jul 20 |
peter |
29 |
#include <memory> |
3948 |
20 Jul 20 |
peter |
30 |
#include <thread> |
3948 |
20 Jul 20 |
peter |
31 |
#include <vector> |
3399 |
30 Mar 15 |
peter |
32 |
|
3399 |
30 Mar 15 |
peter |
33 |
using namespace theplu::yat; |
3399 |
30 Mar 15 |
peter |
34 |
using utility::PriorityQueue; |
3399 |
30 Mar 15 |
peter |
35 |
|
3399 |
30 Mar 15 |
peter |
36 |
class Worker |
3399 |
30 Mar 15 |
peter |
37 |
{ |
3399 |
30 Mar 15 |
peter |
38 |
public: |
3399 |
30 Mar 15 |
peter |
39 |
Worker(PriorityQueue<std::string>& q, test::Suite& suite) |
3399 |
30 Mar 15 |
peter |
40 |
: queue_(q), suite_(suite) {} |
3399 |
30 Mar 15 |
peter |
41 |
|
3399 |
30 Mar 15 |
peter |
42 |
void operator()(void) |
3399 |
30 Mar 15 |
peter |
43 |
{ |
3399 |
30 Mar 15 |
peter |
44 |
try { |
3399 |
30 Mar 15 |
peter |
45 |
while (true) { |
3399 |
30 Mar 15 |
peter |
46 |
std::string str; |
3399 |
30 Mar 15 |
peter |
47 |
queue_.pop(str); |
3399 |
30 Mar 15 |
peter |
48 |
if (str=="poison pill") { |
3399 |
30 Mar 15 |
peter |
49 |
queue_.push(str); |
3399 |
30 Mar 15 |
peter |
50 |
break; |
3399 |
30 Mar 15 |
peter |
51 |
} |
3399 |
30 Mar 15 |
peter |
52 |
suite_.out() << str; |
3399 |
30 Mar 15 |
peter |
53 |
} |
3399 |
30 Mar 15 |
peter |
54 |
} |
3399 |
30 Mar 15 |
peter |
55 |
catch (std::runtime_error& e) { |
3399 |
30 Mar 15 |
peter |
56 |
std::cerr << e.what() << "\n"; |
3399 |
30 Mar 15 |
peter |
57 |
suite_.add(false); |
3399 |
30 Mar 15 |
peter |
58 |
} |
3399 |
30 Mar 15 |
peter |
59 |
} |
3399 |
30 Mar 15 |
peter |
60 |
|
3399 |
30 Mar 15 |
peter |
61 |
private: |
3399 |
30 Mar 15 |
peter |
62 |
PriorityQueue<std::string>& queue_; |
3399 |
30 Mar 15 |
peter |
63 |
test::Suite& suite_; |
3399 |
30 Mar 15 |
peter |
64 |
}; |
3399 |
30 Mar 15 |
peter |
65 |
|
3399 |
30 Mar 15 |
peter |
66 |
|
3399 |
30 Mar 15 |
peter |
67 |
int main(int argc, char* argv[]) |
3399 |
30 Mar 15 |
peter |
68 |
{ |
3399 |
30 Mar 15 |
peter |
69 |
test::Suite suite(argc, argv); |
3399 |
30 Mar 15 |
peter |
70 |
PriorityQueue<std::string> queue; |
3399 |
30 Mar 15 |
peter |
71 |
queue.push("To "); |
3399 |
30 Mar 15 |
peter |
72 |
queue.push("be "); |
3399 |
30 Mar 15 |
peter |
73 |
queue.push("or "); |
3399 |
30 Mar 15 |
peter |
74 |
queue.push("not "); |
3399 |
30 Mar 15 |
peter |
75 |
queue.push("to "); |
3399 |
30 Mar 15 |
peter |
76 |
queue.push("be "); |
3399 |
30 Mar 15 |
peter |
77 |
queue.push("!!!"); |
3399 |
30 Mar 15 |
peter |
78 |
|
3399 |
30 Mar 15 |
peter |
79 |
std::string str; |
3399 |
30 Mar 15 |
peter |
80 |
if (queue.try_pop(str)) |
3399 |
30 Mar 15 |
peter |
81 |
suite.out() << "popped one element in main thread: " << str << "\n"; |
3399 |
30 Mar 15 |
peter |
82 |
|
3948 |
20 Jul 20 |
peter |
83 |
std::vector<std::thread> threads; |
3948 |
20 Jul 20 |
peter |
84 |
for (size_t i=0; i<2; ++i) |
3948 |
20 Jul 20 |
peter |
85 |
threads.push_back(std::thread(Worker(queue, suite))); |
3399 |
30 Mar 15 |
peter |
86 |
|
3399 |
30 Mar 15 |
peter |
87 |
sleep(2); |
3399 |
30 Mar 15 |
peter |
88 |
queue.push("poison pill"); |
3399 |
30 Mar 15 |
peter |
89 |
|
3948 |
20 Jul 20 |
peter |
90 |
for (size_t i=0; i<threads.size(); ++i) |
3948 |
20 Jul 20 |
peter |
91 |
threads[i].join(); |
3403 |
31 Mar 15 |
peter |
92 |
|
3403 |
31 Mar 15 |
peter |
93 |
typedef std::greater<std::string> MyComp; |
3403 |
31 Mar 15 |
peter |
94 |
MyComp comp; |
3403 |
31 Mar 15 |
peter |
95 |
PriorityQueue<std::string, MyComp> queue2(comp); |
3403 |
31 Mar 15 |
peter |
96 |
queue2.push("hello"); |
3832 |
06 Aug 19 |
peter |
97 |
if (queue2.size() != 1) { |
3832 |
06 Aug 19 |
peter |
98 |
suite.add(false); |
3832 |
06 Aug 19 |
peter |
99 |
suite.err() << "incorrect size: " << queue2.size() << "; expected 1\n"; |
3832 |
06 Aug 19 |
peter |
100 |
} |
3832 |
06 Aug 19 |
peter |
101 |
queue2.push("hello"); |
3832 |
06 Aug 19 |
peter |
102 |
if (queue2.size() != 2) { |
3832 |
06 Aug 19 |
peter |
103 |
suite.add(false); |
3832 |
06 Aug 19 |
peter |
104 |
suite.err() << "incorrect size: " << queue2.size() << "; expected 2\n"; |
3832 |
06 Aug 19 |
peter |
105 |
} |
3403 |
31 Mar 15 |
peter |
106 |
|
3399 |
30 Mar 15 |
peter |
107 |
return suite.return_value(); |
3399 |
30 Mar 15 |
peter |
108 |
} |