4043 |
01 Mar 21 |
peter |
// $Id$ |
4043 |
01 Mar 21 |
peter |
2 |
|
4043 |
01 Mar 21 |
peter |
3 |
/* |
4043 |
01 Mar 21 |
peter |
Copyright (C) 2021 Peter Johansson |
4043 |
01 Mar 21 |
peter |
5 |
|
4043 |
01 Mar 21 |
peter |
This file is part of the yat library, http://dev.thep.lu.se/yat |
4043 |
01 Mar 21 |
peter |
7 |
|
4043 |
01 Mar 21 |
peter |
The yat library is free software; you can redistribute it and/or |
4043 |
01 Mar 21 |
peter |
modify it under the terms of the GNU General Public License as |
4043 |
01 Mar 21 |
peter |
published by the Free Software Foundation; either version 3 of the |
4043 |
01 Mar 21 |
peter |
License, or (at your option) any later version. |
4043 |
01 Mar 21 |
peter |
12 |
|
4043 |
01 Mar 21 |
peter |
The yat library is distributed in the hope that it will be useful, |
4043 |
01 Mar 21 |
peter |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
4043 |
01 Mar 21 |
peter |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
4043 |
01 Mar 21 |
peter |
General Public License for more details. |
4043 |
01 Mar 21 |
peter |
17 |
|
4043 |
01 Mar 21 |
peter |
You should have received a copy of the GNU General Public License |
4043 |
01 Mar 21 |
peter |
along with yat. If not, see <http://www.gnu.org/licenses/>. |
4043 |
01 Mar 21 |
peter |
20 |
*/ |
4043 |
01 Mar 21 |
peter |
21 |
|
4043 |
01 Mar 21 |
peter |
22 |
#include <config.h> |
4043 |
01 Mar 21 |
peter |
23 |
|
4043 |
01 Mar 21 |
peter |
24 |
#include "Suite.h" |
4043 |
01 Mar 21 |
peter |
25 |
|
4043 |
01 Mar 21 |
peter |
26 |
#include "yat/utility/Queue.h" |
4043 |
01 Mar 21 |
peter |
27 |
|
4043 |
01 Mar 21 |
peter |
28 |
#include <iostream> |
4043 |
01 Mar 21 |
peter |
29 |
#include <thread> |
4043 |
01 Mar 21 |
peter |
30 |
#include <vector> |
4043 |
01 Mar 21 |
peter |
31 |
|
4043 |
01 Mar 21 |
peter |
32 |
using namespace theplu::yat; |
4043 |
01 Mar 21 |
peter |
33 |
using utility::Queue; |
4043 |
01 Mar 21 |
peter |
34 |
|
4043 |
01 Mar 21 |
peter |
35 |
class Producer |
4043 |
01 Mar 21 |
peter |
36 |
{ |
4043 |
01 Mar 21 |
peter |
37 |
public: |
4043 |
01 Mar 21 |
peter |
38 |
Producer(Queue<int>& q, size_t b, size_t e) |
4043 |
01 Mar 21 |
peter |
39 |
: queue_(q), begin_(b), end_(e) {} |
4043 |
01 Mar 21 |
peter |
40 |
|
4043 |
01 Mar 21 |
peter |
41 |
void operator()(void) |
4043 |
01 Mar 21 |
peter |
42 |
{ |
4043 |
01 Mar 21 |
peter |
43 |
for (size_t i=begin_; i<end_; ++i) { |
4043 |
01 Mar 21 |
peter |
44 |
queue_.push(i); |
4043 |
01 Mar 21 |
peter |
45 |
} |
4043 |
01 Mar 21 |
peter |
46 |
} |
4043 |
01 Mar 21 |
peter |
47 |
|
4043 |
01 Mar 21 |
peter |
48 |
private: |
4043 |
01 Mar 21 |
peter |
49 |
Queue<int>& queue_; |
4043 |
01 Mar 21 |
peter |
50 |
size_t begin_; |
4043 |
01 Mar 21 |
peter |
51 |
size_t end_; |
4043 |
01 Mar 21 |
peter |
52 |
}; |
4043 |
01 Mar 21 |
peter |
53 |
|
4043 |
01 Mar 21 |
peter |
54 |
|
4043 |
01 Mar 21 |
peter |
55 |
class Consumer |
4043 |
01 Mar 21 |
peter |
56 |
{ |
4043 |
01 Mar 21 |
peter |
57 |
public: |
4043 |
01 Mar 21 |
peter |
58 |
Consumer(Queue<int>& q, std::vector<int>& r, size_t n) |
4043 |
01 Mar 21 |
peter |
59 |
: queue_(q), result_(r), n_(n) {} |
4043 |
01 Mar 21 |
peter |
60 |
|
4043 |
01 Mar 21 |
peter |
61 |
void operator()(void) |
4043 |
01 Mar 21 |
peter |
62 |
{ |
4043 |
01 Mar 21 |
peter |
63 |
int x = 0; |
4043 |
01 Mar 21 |
peter |
64 |
while (result_.size() < n_) { |
4043 |
01 Mar 21 |
peter |
65 |
queue_.pop(x); |
4043 |
01 Mar 21 |
peter |
66 |
result_.push_back(x); |
4043 |
01 Mar 21 |
peter |
67 |
} |
4043 |
01 Mar 21 |
peter |
68 |
} |
4043 |
01 Mar 21 |
peter |
69 |
|
4043 |
01 Mar 21 |
peter |
70 |
private: |
4043 |
01 Mar 21 |
peter |
71 |
Queue<int>& queue_; |
4043 |
01 Mar 21 |
peter |
72 |
std::vector<int>& result_; |
4043 |
01 Mar 21 |
peter |
73 |
size_t n_; |
4043 |
01 Mar 21 |
peter |
74 |
}; |
4043 |
01 Mar 21 |
peter |
75 |
|
4043 |
01 Mar 21 |
peter |
76 |
|
4043 |
01 Mar 21 |
peter |
77 |
int main(int argc,char* argv[]) |
4043 |
01 Mar 21 |
peter |
78 |
{ |
4043 |
01 Mar 21 |
peter |
79 |
test::Suite suite(argc, argv); |
4043 |
01 Mar 21 |
peter |
80 |
Queue<int> queue; |
4043 |
01 Mar 21 |
peter |
81 |
size_t N = 4; |
4043 |
01 Mar 21 |
peter |
82 |
queue.capacity(N); |
4043 |
01 Mar 21 |
peter |
83 |
|
4043 |
01 Mar 21 |
peter |
84 |
size_t c = queue.capacity(); |
4043 |
01 Mar 21 |
peter |
85 |
if (c != N) { |
4043 |
01 Mar 21 |
peter |
86 |
suite.err() << "error: capacity: " << c << "; expected N\n"; |
4043 |
01 Mar 21 |
peter |
87 |
suite.add(false); |
4043 |
01 Mar 21 |
peter |
88 |
} |
4043 |
01 Mar 21 |
peter |
89 |
|
4043 |
01 Mar 21 |
peter |
90 |
std::vector<std::thread> threads; |
4043 |
01 Mar 21 |
peter |
91 |
threads.push_back(std::thread(Producer(queue, 0, 10))); |
4043 |
01 Mar 21 |
peter |
92 |
std::this_thread::sleep_for(std::chrono::seconds(1)); |
4043 |
01 Mar 21 |
peter |
93 |
size_t size = queue.size(); |
4043 |
01 Mar 21 |
peter |
94 |
suite.out() << "size: " << size << "\n"; |
4043 |
01 Mar 21 |
peter |
95 |
if (size > N) { |
4043 |
01 Mar 21 |
peter |
96 |
suite.add(false); |
4043 |
01 Mar 21 |
peter |
97 |
suite.err() << "error: size greater than " << N << "\n"; |
4043 |
01 Mar 21 |
peter |
98 |
} |
4043 |
01 Mar 21 |
peter |
99 |
|
4043 |
01 Mar 21 |
peter |
100 |
if (queue.try_push(42)) { |
4043 |
01 Mar 21 |
peter |
101 |
suite.err() << "try_pop(42) returned true unexpectedly\n"; |
4043 |
01 Mar 21 |
peter |
102 |
suite.add(false); |
4043 |
01 Mar 21 |
peter |
103 |
} |
4043 |
01 Mar 21 |
peter |
104 |
|
4043 |
01 Mar 21 |
peter |
105 |
threads.push_back(std::thread(Producer(queue, 0, 10))); |
4043 |
01 Mar 21 |
peter |
106 |
std::vector<int> result; |
4043 |
01 Mar 21 |
peter |
107 |
threads.push_back(std::thread(Consumer(queue, result, 2*10))); |
4043 |
01 Mar 21 |
peter |
108 |
|
4043 |
01 Mar 21 |
peter |
109 |
for (size_t i=0; i<threads.size(); ++i) |
4043 |
01 Mar 21 |
peter |
110 |
threads[i].join(); |
4043 |
01 Mar 21 |
peter |
111 |
|
4043 |
01 Mar 21 |
peter |
112 |
if (result.size() != 20) { |
4043 |
01 Mar 21 |
peter |
113 |
suite.add(false); |
4043 |
01 Mar 21 |
peter |
114 |
suite.err() << "size: " << result.size() << "\n"; |
4043 |
01 Mar 21 |
peter |
115 |
suite.err() << "error: incorrect size\n"; |
4043 |
01 Mar 21 |
peter |
116 |
} |
4043 |
01 Mar 21 |
peter |
117 |
std::cout << "result: "; |
4043 |
01 Mar 21 |
peter |
118 |
std::copy(result.begin(), result.end(), |
4043 |
01 Mar 21 |
peter |
119 |
std::ostream_iterator<int>(suite.out(), " ")); |
4043 |
01 Mar 21 |
peter |
120 |
std::cout << "\n"; |
4043 |
01 Mar 21 |
peter |
121 |
|
4043 |
01 Mar 21 |
peter |
122 |
return suite.return_value(); |
4043 |
01 Mar 21 |
peter |
123 |
} |