From: Martin Quinson Date: Sun, 20 Jan 2019 00:33:37 +0000 (+0100) Subject: new (failing) example about on_exit and on_destruction X-Git-Tag: v3_22~531 X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/commitdiff_plain/4e4e76c75796289a96650907c73c120485c46f4e?ds=sidebyside new (failing) example about on_exit and on_destruction --- diff --git a/examples/s4u/actor-exiting/s4u-actor-exiting.cpp b/examples/s4u/actor-exiting/s4u-actor-exiting.cpp new file mode 100644 index 0000000000..ac5042de55 --- /dev/null +++ b/examples/s4u/actor-exiting/s4u-actor-exiting.cpp @@ -0,0 +1,63 @@ +/* Copyright (c) 2017-2019. The SimGrid Team. All rights reserved. */ + +/* This program is free software; you can redistribute it and/or modify it + * under the terms of the license (GNU LGPL) which comes with this package. */ + +/* There is two very different ways of being informed when an actor exits. + * + * The this_actor::on_exit() function allows to register a function to be + * executed when this very actor exits. The registered function will run + * when this actor terminates (either because its main function returns, or + * because it's killed in any way). No simcall are allowed here: your actor + * is dead already, so it cannot interact with its environment in any way + * (network, executions, disks, etc). + * + * Usually, the functions registered in this_actor::on_exit() are in charge + * of releasing any memory allocated by the actor during its execution. + * + * The other way of getting informed when an actor dies is to connect a + * function in the Actor::on_destruction signal, that is shared between + * all actors. Callbacks to this signal are executed for each terminating + * actors, no matter what. This is useful in many cases, in particular + * when developping SimGrid plugins. + * + * In both cases, you can stack more than one callback in the signal. + * They will all be executed in the registration order. + */ + +#include + +XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_actor_exiting, "Messages specific for this s4u example"); + +static void actor_a() +{ + // Register a lambda function to be executed once it stops + simgrid::s4u::this_actor::on_exit([](int, void*) { XBT_INFO("I stop now"); }, nullptr); + + simgrid::s4u::this_actor::execute(1e9); +} + +static void actor_b() +{ + simgrid::s4u::this_actor::execute(2e9); +} + +int main(int argc, char* argv[]) +{ + simgrid::s4u::Engine e(&argc, argv); + xbt_assert(argc == 2, "Usage: %s platform_file\n\tExample: %s msg_platform.xml\n", argv[0], argv[0]); + + e.load_platform(argv[1]); /* - Load the platform description */ + + /* Register a callback in the Actor::on_destruction signal. It will be called for every terminated actors */ + simgrid::s4u::Actor::on_destruction.connect( + [](simgrid::s4u::ActorPtr actor) { XBT_INFO("Actor %s stops now", actor->get_cname()); }); + + /* Create some actors */ + simgrid::s4u::Actor::create("A", simgrid::s4u::Host::by_name("Tremblay"), actor_a); + simgrid::s4u::Actor::create("B", simgrid::s4u::Host::by_name("Fafard"), actor_b); + + e.run(); /* - Run the simulation */ + + return 0; +} diff --git a/examples/s4u/actor-exiting/s4u-actor-exiting.tesh b/examples/s4u/actor-exiting/s4u-actor-exiting.tesh new file mode 100644 index 0000000000..d5a15ed8d2 --- /dev/null +++ b/examples/s4u/actor-exiting/s4u-actor-exiting.tesh @@ -0,0 +1,6 @@ +#!/usr/bin/env tesh + +$ $SG_TEST_EXENV ${bindir:=.}/s4u-actor-exiting ${platfdir}/small_platform.xml "--log=root.fmt:[%10.6r]%e(%P@%h)%e%m%n" +> [ 10.194200] (A@Tremblay) I stop now +> [ 10.194200] (maestro@) Actor A stops now +> [ 26.213694] (maestro@) Actor B stops now