source: trunk/test/queue.cc

Last change on this file was 3999, checked in by Peter, 3 years ago

update copyright years

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 1.7 KB
Line 
1// $Id: queue.cc 3999 2020-10-08 23:22:32Z peter $
2
3/*
4  Copyright (C) 2013, 2020 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 Worker
36{
37public:
38  Worker(Queue<std::string>& q)
39  : queue_(q) {}
40
41  void operator()(void)
42  {
43    std::string str = " ";
44    while (str.size()) {
45      queue_.pop(str);
46      if (str.size()) {
47        queue_.push(str.substr(1));
48        if (str.size()>1)
49          queue_.push(str.substr(0,str.size()-1));
50      }
51      sleep(1);
52    }
53  }
54
55private:
56  Queue<std::string>& queue_;
57};
58
59int main(int argc,char* argv[])
60{
61  test::Suite suite(argc, argv);
62  Queue<std::string> queue;
63  queue.push("Peter");
64
65  std::vector<std::thread> threads;
66  threads.push_back(std::thread(Worker(queue)));
67  threads.push_back(std::thread(Worker(queue)));
68  threads.push_back(std::thread(Worker(queue)));
69  threads.push_back(std::thread(Worker(queue)));
70
71  for (size_t i=0; i<threads.size(); ++i)
72    threads[i].join();
73
74  Queue<std::string> queue2(queue);
75
76  queue2 = queue;
77
78  return suite.return_value();
79}
Note: See TracBrowser for help on using the repository browser.