Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
snake case this bool
[simgrid.git] / examples / s4u / actor-exiting / s4u-actor-exiting.cpp
1 /* Copyright (c) 2017-2019. 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 /* There is two very different ways of being informed when an actor exits.
7  *
8  * The this_actor::on_exit() function allows to register a function to be
9  * executed when this very actor exits. The registered function will run
10  * when this actor terminates (either because its main function returns, or
11  * because it's killed in any way). No simcall are allowed here: your actor
12  * is dead already, so it cannot interact with its environment in any way
13  * (network, executions, disks, etc).
14  *
15  * Usually, the functions registered in this_actor::on_exit() are in charge
16  * of releasing any memory allocated by the actor during its execution.
17  *
18  * The other way of getting informed when an actor dies is to connect a
19  * function in the Actor::on_destruction signal, that is shared between
20  * all actors. Callbacks to this signal are executed for each terminating
21  * actors, no matter what. This is useful in many cases, in particular
22  * when developping SimGrid plugins.
23  *
24  * In both cases, you can stack more than one callback in the signal.
25  * They will all be executed in the registration order.
26  */
27
28 #include <simgrid/s4u.hpp>
29
30 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_actor_exiting, "Messages specific for this s4u example");
31
32 static void actor_a()
33 {
34   // Register a lambda function to be executed once it stops
35   simgrid::s4u::this_actor::on_exit([](int, void*) { XBT_INFO("I stop now"); }, nullptr);
36
37   simgrid::s4u::this_actor::execute(1e9);
38 }
39
40 static void actor_b()
41 {
42   simgrid::s4u::this_actor::execute(2e9);
43 }
44
45 int main(int argc, char* argv[])
46 {
47   simgrid::s4u::Engine e(&argc, argv);
48   xbt_assert(argc == 2, "Usage: %s platform_file\n\tExample: %s msg_platform.xml\n", argv[0], argv[0]);
49
50   e.load_platform(argv[1]); /* - Load the platform description */
51
52   /* Register a callback in the Actor::on_destruction signal. It will be called for every terminated actors */
53   simgrid::s4u::Actor::on_destruction.connect(
54       [](simgrid::s4u::ActorPtr actor) { XBT_INFO("Actor %s stops now", actor->get_cname()); });
55
56   /* Create some actors */
57   simgrid::s4u::Actor::create("A", simgrid::s4u::Host::by_name("Tremblay"), actor_a);
58   simgrid::s4u::Actor::create("B", simgrid::s4u::Host::by_name("Fafard"), actor_b);
59
60   e.run(); /* - Run the simulation */
61
62   return 0;
63 }