Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
sonar: also ignore S5271 for the examples in C++
[simgrid.git] / teshsuite / s4u / actor-suspend / actor-suspend.cpp
1 /* Copyright (c) 2020. 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 occuring when suspending an actor that will be executed later in the same scheduling round
8
9 #include <iostream>
10 #include <simgrid/s4u.hpp>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <vector>
14
15 XBT_LOG_NEW_DEFAULT_CATEGORY(mwe, "Minimum Working Example");
16
17 simgrid::s4u::ActorPtr receiver;
18
19 class Receiver {
20
21 public:
22   void operator()()
23   {
24     XBT_INFO("Starting.");
25     auto mailbox = simgrid::s4u::Mailbox::by_name("receiver");
26     int data     = *(int*)mailbox->get();
27     XBT_INFO("Got %d at the end", data);
28   }
29 };
30
31 class Suspender {
32
33 public:
34   void operator()()
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     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
56   simgrid::s4u::Engine* engine = new simgrid::s4u::Engine(&argc, argv);
57
58   engine->load_platform(argv[1]);
59   simgrid::s4u::Host* host = simgrid::s4u::Host::by_name("Tremblay");
60
61   simgrid::s4u::Actor::create("Suspender", host, Suspender());
62   receiver = simgrid::s4u::Actor::create("Receiver", host, Receiver());
63
64   engine->run();
65
66   return 0;
67 }