source: trunk/test/scheduler.cc @ 3826

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

function to access number of threads in Scheduler. closes #914

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 2.2 KB
Line 
1// $Id: scheduler.cc 3826 2019-07-18 06:47:27Z peter $
2
3/*
4  Copyright (C) 2014, 2015, 2017 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  }
48private:
49  int time_;
50};
51
52
53int main(int argc, char* argv[])
54{
55  test::Suite suite(argc, argv);
56
57  Scheduler scheduler(2);
58  int n_threads = scheduler.threads();
59  if (n_threads != 2) {
60    suite.add(false);
61    suite.err() << "threads(): " << n_threads << " - expected: 2\n";
62  }
63  std::vector<boost::shared_ptr<Sleeper> > sleeper;
64  sleeper.reserve(4);
65  sleeper.push_back(boost::make_shared<Sleeper>(1));
66  sleeper.push_back(boost::make_shared<Sleeper>(10));
67  scheduler.submit(sleeper[1]);
68  sleeper.push_back(boost::make_shared<Sleeper>(2));
69  scheduler.add_dependency(sleeper[2], sleeper[0]);
70  sleeper.push_back(boost::make_shared<Sleeper>(3));
71
72  // not strictly needed since sleeper2 depends on sleeper0,
73  // but... this is a test
74  scheduler.add_dependency(sleeper[3], sleeper[0]);
75  scheduler.add_dependency(sleeper[3], sleeper[1]);
76  scheduler.add_dependency(sleeper[3], sleeper[2]);
77
78  scheduler.submit(sleeper[3]);
79  suite.out() << "jobs: " << scheduler.jobs() << "\n";
80  scheduler.wait();
81  return suite.return_value();
82}
Note: See TracBrowser for help on using the repository browser.