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