forked from luteberget/simcpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example-twocars.cpp
60 lines (44 loc) · 1.24 KB
/
example-twocars.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Copyright © 2021 Bjørnar Steinnes Luteberget
// Licensed under the MIT license. See the LICENSE file for details.
#include <cstdio>
#include <string>
#include "simcpp.h"
class Car : public simcpp::Process {
public:
explicit Car(simcpp::SimulationPtr sim, std::string name)
: Process(sim), target_time(sim->get_now() + 100.0), name(name) {}
bool Run() override {
auto sim = this->sim.lock();
PT_BEGIN();
while (sim->get_now() < target_time) {
PROC_WAIT_FOR(sim->timeout(5.0));
printf("Car %s running at %g.\n", name.c_str(), sim->get_now());
}
PT_END();
}
private:
bool finished = false;
double target_time;
std::string name;
};
class TwoCars : public simcpp::Process {
public:
explicit TwoCars(simcpp::SimulationPtr sim) : Process(sim) {}
bool Run() override {
auto sim = this->sim.lock();
PT_BEGIN();
printf("Starting car C1.\n");
PROC_WAIT_FOR(sim->start_process<Car>("C1"));
printf("Finished car C1.\n");
printf("Starting car C2.\n");
PROC_WAIT_FOR(sim->start_process<Car>("C2"));
printf("Finished car C2.\n");
PT_END();
}
};
int main() {
auto sim = simcpp::Simulation::create();
sim->start_process<TwoCars>();
sim->advance_by(10000);
return 0;
}