source: trunk/test/scheduler6.cc @ 3827

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

refs #916. add test case when using Scheduler after calling wait().

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 1.9 KB
Line 
1// $Id: scheduler6.cc 3827 2019-07-19 07:31:16Z peter $
2
3/*
4  Copyright (C) 2019 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/Scheduler.h"
27
28#include <boost/make_shared.hpp>
29#include <boost/shared_ptr.hpp>
30
31#include <vector>
32
33using namespace theplu::yat;
34using namespace theplu::yat::utility;
35
36class Sleeper : public Scheduler::Job
37{
38public:
39  Sleeper(int t) : time_(t), have_slept_(false) {}
40  void operator()(void)
41  {
42    sleep(time_);
43    std::cout << "Have slept for " << time_ << " seconds" << std::endl;
44    have_slept_ = true;
45  }
46private:
47  int time_;
48public:
49  bool have_slept_;
50};
51
52
53int main(int argc, char* argv[])
54{
55  test::Suite suite(argc, argv);
56
57  Scheduler scheduler(2);
58  std::vector<boost::shared_ptr<Sleeper> > sleeper;
59  sleeper.reserve(5);
60  sleeper.push_back(boost::make_shared<Sleeper>(1));
61  sleeper.push_back(boost::make_shared<Sleeper>(4));
62  sleeper.push_back(boost::make_shared<Sleeper>(2));
63  sleeper.push_back(boost::make_shared<Sleeper>(3));
64  for (size_t i=0; i<sleeper.size(); ++i)
65    scheduler.submit(sleeper[i]);
66  scheduler.wait();
67  sleeper.push_back(boost::make_shared<Sleeper>(1));
68  scheduler.submit(sleeper.back());
69  scheduler.wait();
70
71  if (sleeper.back()->have_slept_ == false) {
72    suite.add(false);
73    suite.err() << "last sleeper never slept\n";
74  }
75
76
77  return suite.return_value();
78}
Note: See TracBrowser for help on using the repository browser.