source: trunk/test/queue2.cc @ 3063

Last change on this file since 3063 was 3063, checked in by Peter, 8 years ago

add another test for new class Queue

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 1.9 KB
Line 
1// $Id: queue2.cc 3063 2013-07-02 05:28:39Z peter $
2
3/*
4  Copyright (C) 2013 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 "yat/utility/Queue.h"
25
26#include "Suite.h"
27
28#include <boost/thread.hpp>
29
30#include <iostream>
31
32using namespace theplu::yat;
33using utility::Queue;
34
35class Worker
36{
37public:
38  Worker(Queue<std::string>& q, test::Suite& suite)
39    : queue_(q), suite_(suite) {}
40
41  void operator()(void)
42  {
43    try {
44      while (true) {
45        std::string str;
46        queue_.pop(str);
47        if (str=="poison pill") {
48          queue_.push(str);
49          break;
50        }
51        suite_.out() << str;
52        boost::this_thread::interruption_point();
53      }
54    }
55    catch (std::runtime_error& e) {
56      std::cerr << e.what() << "\n";
57      suite_.add(false);
58    }
59  }
60
61private:
62  Queue<std::string>& queue_;
63  test::Suite& suite_;
64};
65
66
67int main(int argc, char* argv[])
68{
69  test::Suite suite(argc, argv);
70  Queue<std::string> queue;
71  queue.push("To ");
72  queue.push("be ");
73  queue.push("or ");
74  queue.push("not ");
75  queue.push("to ");
76  queue.push("be ");
77  queue.push("!!!");
78
79  boost::thread_group workers;
80  workers.create_thread(Worker(queue, suite));
81  workers.create_thread(Worker(queue, suite));
82
83  sleep(2);
84  queue.push("poison pill");
85
86  workers.join_all();
87  suite.out() << "\n";
88  return suite.return_value();
89}
Note: See TracBrowser for help on using the repository browser.