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