X-Git-Url: http://bilbo.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/e599303392d7cf5795927906bc44619ab60dfcaf..c10c571a634491a941c1b39c50020da8d709a940:/teshsuite/s4u/actor-suspend/actor-suspend.cpp diff --git a/teshsuite/s4u/actor-suspend/actor-suspend.cpp b/teshsuite/s4u/actor-suspend/actor-suspend.cpp index a75cc86ed5..91aad4daa1 100644 --- a/teshsuite/s4u/actor-suspend/actor-suspend.cpp +++ b/teshsuite/s4u/actor-suspend/actor-suspend.cpp @@ -1,42 +1,38 @@ -/* Copyright (c) 2020. The SimGrid Team. All rights reserved. */ +/* Copyright (c) 2020-2023. 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. */ // This is the MWE of https://framagit.org/simgrid/simgrid/-/issues/50 -// The problem was occuring when suspending an actor that will be executed later in the same scheduling round +// The problem was occurring when suspending an actor that will be executed later in the same scheduling round +#include +#include #include #include -#include -#include #include XBT_LOG_NEW_DEFAULT_CATEGORY(mwe, "Minimum Working Example"); -simgrid::s4u::ActorPtr receiver; - class Receiver { - public: - void operator()() + void operator()() const { XBT_INFO("Starting."); - auto mailbox = simgrid::s4u::Mailbox::by_name("receiver"); - int data = *(int*)mailbox->get(); + auto* mailbox = simgrid::s4u::Mailbox::by_name("receiver"); + int data = *mailbox->get(); XBT_INFO("Got %d at the end", data); } }; class Suspender { + const simgrid::s4u::ActorPtr& receiver; public: - void operator()() - { - - // If we sleep a bit here, this MWE works because the suspender is not trying to suspend someone executed later in - // the same scheduling round simgrid::s4u::this_actor::sleep_for(0.01); + explicit Suspender(const simgrid::s4u::ActorPtr& receiver) : receiver(receiver) {} + void operator()() const + { XBT_INFO("Suspend the receiver..."); receiver->suspend(); XBT_INFO("Resume the receiver..."); @@ -46,8 +42,8 @@ public: simgrid::s4u::this_actor::sleep_for(10); XBT_INFO("Sending a message to the receiver..."); - auto mailbox = simgrid::s4u::Mailbox::by_name("receiver"); - int data = 42; + auto* mailbox = simgrid::s4u::Mailbox::by_name("receiver"); + static int data = 42; mailbox->put(&data, 4); XBT_INFO("Done!"); @@ -56,16 +52,16 @@ public: int main(int argc, char** argv) { + simgrid::s4u::Engine engine(&argc, argv); - simgrid::s4u::Engine* engine = new simgrid::s4u::Engine(&argc, argv); - - engine->load_platform(argv[1]); - simgrid::s4u::Host* host = simgrid::s4u::Host::by_name("Tremblay"); + engine.load_platform(argv[1]); + simgrid::s4u::Host* host = engine.host_by_name("Tremblay"); - simgrid::s4u::Actor::create("Suspender", host, Suspender()); + simgrid::s4u::ActorPtr receiver; + simgrid::s4u::Actor::create("Suspender", host, Suspender(receiver)); receiver = simgrid::s4u::Actor::create("Receiver", host, Receiver()); - engine->run(); + engine.run(); return 0; }