Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Clang-tidy: readability-qualified-auto.
[simgrid.git] / docs / source / tuto_mc / ndet-receive-s4u.cpp
1 /* Copyright (c) 2010-2021. 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 /******************** Non-deterministic message ordering  *********************/
7 /* Server assumes a fixed order in the reception of messages from its clients */
8 /* which is incorrect because the message ordering is non-deterministic       */
9 /******************************************************************************/
10
11 #include <simgrid/s4u.hpp>
12 namespace sg4 = simgrid::s4u;
13
14 constexpr int N = 3;
15
16 XBT_LOG_NEW_DEFAULT_CATEGORY(example, "this example");
17
18 static void server()
19 {
20   auto* mb      = sg4::Mailbox::by_name("mymailbox");
21   int value_got = -1;
22   for (int count = 0; count < N; count++) {
23     int *received = mb->get<int>();
24     value_got = *received;
25     delete received;
26   }
27   xbt_assert(value_got == 3);
28
29   XBT_INFO("OK");
30 }
31
32 static void client(int id)
33 {
34   auto* payload = new int(id);
35   XBT_INFO("Sending %d", id);
36   sg4::Mailbox::by_name("mymailbox")->put(payload, 10000);
37   XBT_INFO("Sent!");
38 }
39
40 int main(int argc, char* argv[])
41 {
42   sg4::Engine e(&argc, argv);
43
44   std::string platform_file = "small_platform.xml";
45   if (argc > 1)
46     platform_file = argv[1];
47   e.load_platform(platform_file);
48
49   sg4::Actor::create("server", sg4::Host::by_name("Tremblay"), server);
50   sg4::Actor::create("client", sg4::Host::by_name("Jupiter"),  client, 1);
51   sg4::Actor::create("client", sg4::Host::by_name("Bourassa"), client, 2);
52   sg4::Actor::create("client", sg4::Host::by_name("Ginette"),  client, 3);
53
54   e.run();
55   return 0;
56 }