source: trunk/test/queue3.cc @ 4043

Last change on this file since 4043 was 4043, checked in by Peter, 9 months ago

Add capacity to Queue and PriorityQueue?, which is a feature to limit
the size of the queue, i.e., a push is waiting until size < capacity
before completing the push.

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