Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
2dd5564daf38fa99d31e5a40f47e4f3fa7ec6257
[simgrid.git] / examples / cpp / actor-daemon / s4u-actor-daemon.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_actor_daemon, "Messages specific for this s4u example");
10
11 /* The worker actor, working for a while before leaving */
12 static void worker()
13 {
14   XBT_INFO("Let's do some work (for 10 sec on Boivin).");
15   sg4::this_actor::execute(980.95e6);
16
17   XBT_INFO("I'm done now. I leave even if it makes the daemon die.");
18 }
19
20 /* The daemon, displaying a message every 3 seconds until all other actors stop */
21 static void my_daemon()
22 {
23   sg4::Actor::self()->daemonize();
24
25   while (sg4::this_actor::get_host()->is_on()) {
26     XBT_INFO("Hello from the infinite loop");
27     sg4::this_actor::sleep_for(3.0);
28   }
29
30   XBT_INFO("I will never reach that point: daemons are killed when regular actors are done");
31 }
32
33 int main(int argc, char* argv[])
34 {
35   sg4::Engine e(&argc, argv);
36
37   e.load_platform(argv[1]);
38   sg4::Actor::create("worker", e.host_by_name("Boivin"), worker);
39   sg4::Actor::create("daemon", e.host_by_name("Tremblay"), my_daemon);
40
41   e.run();
42   return 0;
43 }