Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Sonar smells after last changes.
[simgrid.git] / teshsuite / s4u / actor-suspend / actor-suspend.cpp
1 /* Copyright (c) 2020-2023. 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 // This is the MWE of https://framagit.org/simgrid/simgrid/-/issues/50
7 // The problem was occurring when suspending an actor that will be executed later in the same scheduling round
8
9 #include <cstdio>
10 #include <cstdlib>
11 #include <iostream>
12 #include <simgrid/s4u.hpp>
13 #include <vector>
14
15 XBT_LOG_NEW_DEFAULT_CATEGORY(mwe, "Minimum Working Example");
16
17 class Receiver {
18 public:
19   void operator()() const
20   {
21     XBT_INFO("Starting.");
22     auto mailbox = simgrid::s4u::Mailbox::by_name("receiver");
23     int data     = *mailbox->get<int>();
24     XBT_INFO("Got %d at the end", data);
25   }
26 };
27
28 class Suspender {
29   const simgrid::s4u::ActorPtr& receiver;
30
31 public:
32   explicit Suspender(const simgrid::s4u::ActorPtr& receiver) : receiver(receiver) {}
33
34   void operator()() const
35   {
36     XBT_INFO("Suspend the receiver...");
37     receiver->suspend();
38     XBT_INFO("Resume the receiver...");
39     receiver->resume();
40
41     XBT_INFO("Sleeping 10 sec...");
42     simgrid::s4u::this_actor::sleep_for(10);
43
44     XBT_INFO("Sending a message to the receiver...");
45     auto mailbox = simgrid::s4u::Mailbox::by_name("receiver");
46     static int data = 42;
47     mailbox->put(&data, 4);
48
49     XBT_INFO("Done!");
50   }
51 };
52
53 int main(int argc, char** argv)
54 {
55   simgrid::s4u::Engine engine(&argc, argv);
56
57   engine.load_platform(argv[1]);
58   simgrid::s4u::Host* host = engine.host_by_name("Tremblay");
59
60   simgrid::s4u::ActorPtr receiver;
61   simgrid::s4u::Actor::create("Suspender", host, Suspender(receiver));
62   receiver = simgrid::s4u::Actor::create("Receiver", host, Receiver());
63
64   engine.run();
65
66   return 0;
67 }