Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of framagit.org:simgrid/simgrid
[simgrid.git] / examples / cpp / actor-join / s4u-actor-join.cpp
1 /* Copyright (c) 2017-2021. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include "simgrid/s4u.hpp"
7 namespace sg4 = simgrid::s4u;
8
9 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_test, "Messages specific for this s4u example");
10
11 static void sleeper()
12 {
13   XBT_INFO("Sleeper started");
14   sg4::this_actor::sleep_for(3);
15   XBT_INFO("I'm done. See you!");
16 }
17
18 static void master()
19 {
20   sg4::ActorPtr actor;
21
22   XBT_INFO("Start sleeper");
23   actor = sg4::Actor::create("sleeper from master", sg4::Host::current(), sleeper);
24   XBT_INFO("Join the sleeper (timeout 2)");
25   actor->join(2);
26
27   XBT_INFO("Start sleeper");
28   actor = sg4::Actor::create("sleeper from master", sg4::Host::current(), sleeper);
29   XBT_INFO("Join the sleeper (timeout 4)");
30   actor->join(4);
31
32   XBT_INFO("Start sleeper");
33   actor = sg4::Actor::create("sleeper from master", sg4::Host::current(), sleeper);
34   XBT_INFO("Join the sleeper (timeout 2)");
35   actor->join(2);
36
37   XBT_INFO("Start sleeper");
38   actor = sg4::Actor::create("sleeper from master", sg4::Host::current(), sleeper);
39   XBT_INFO("Waiting 4");
40   sg4::this_actor::sleep_for(4);
41   XBT_INFO("Join the sleeper after its end (timeout 1)");
42   actor->join(1);
43
44   XBT_INFO("Goodbye now!");
45
46   sg4::this_actor::sleep_for(1);
47
48   XBT_INFO("Goodbye now!");
49 }
50
51 int main(int argc, char* argv[])
52 {
53   sg4::Engine e(&argc, argv);
54   xbt_assert(argc == 2, "Usage: %s platform_file\n\tExample: %s ../platforms/small_platform.xml\n", argv[0], argv[0]);
55
56   e.load_platform(argv[1]);
57
58   sg4::Actor::create("master", sg4::Host::by_name("Tremblay"), master);
59
60   e.run();
61
62   XBT_INFO("Simulation time %g", sg4::Engine::get_clock());
63
64   return 0;
65 }