source: trunk/test/scheduler.cc @ 3346

Last change on this file since 3346 was 3346, checked in by Peter, 7 years ago

first version of Scheduler class. refs #800.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 2.0 KB
Line 
1// $Id: scheduler.cc 3346 2014-11-06 13:16:34Z peter $
2
3/*
4  Copyright (C) 2014 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 <cstdlib>
32#include <iostream>
33#include <set>
34#include <unistd.h>
35
36using namespace theplu::yat;
37using namespace theplu::yat::utility;
38
39class Sleeper : public Scheduler::Job
40{
41public:
42  Sleeper(int t) : time_(t) {}
43  void operator()(void)
44  {
45    sleep(time_);
46    std::cout << "Have slept for " << time_ << " seconds" << std::endl;
47    if (time_==2)
48      submit(boost::make_shared<Sleeper>(1));
49  }
50private:
51  int time_;
52};
53
54
55int main(int argc, char* argv[])
56{
57  test::Suite suite(argc, argv);
58
59  Scheduler scheduler(2);
60  std::vector<boost::shared_ptr<Sleeper> > sleeper;
61  sleeper.reserve(4);
62  sleeper.push_back(boost::make_shared<Sleeper>(1));
63  sleeper.push_back(boost::make_shared<Sleeper>(10));
64  sleeper.push_back(boost::make_shared<Sleeper>(2));
65  sleeper.back()->add_dependency(sleeper[0]);
66  sleeper.push_back(boost::make_shared<Sleeper>(3));
67
68  // not strictly needed since sleeper2 depends on sleeper0,
69  // but... this is a test
70  sleeper.back()->add_dependency(sleeper[0]);
71  sleeper.back()->add_dependency(sleeper[1]);
72  sleeper.back()->add_dependency(sleeper[2]);
73
74  scheduler.submit(sleeper[3]);
75  scheduler.launch();
76  return suite.return_value();
77}
Note: See TracBrowser for help on using the repository browser.