Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Jenkins::Flag2: use ninja if avail; verbose builds
[simgrid.git] / examples / cpp / mc-bugged2 / s4u-mc-bugged2.cpp
1 /* Copyright (c) 2010-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 /******************** 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/modelchecker.h>
12 #include <simgrid/s4u.hpp>
13
14 XBT_LOG_NEW_DEFAULT_CATEGORY(example, "this example");
15 namespace sg4 = simgrid::s4u;
16
17 static void server()
18 {
19   auto received1 = sg4::Mailbox::by_name("mymailbox")->get_unique<int>();
20   long val1 = *received1;
21
22   auto received2 = sg4::Mailbox::by_name("mymailbox")->get_unique<int>();
23   long val2 = *received2;
24
25   XBT_INFO("First pair received: %ld %ld", val1, val2);
26
27   MC_assert(std::min(val1, val2) == 1); // if the two messages of the second client arrive first, this is violated.
28
29   received1 = sg4::Mailbox::by_name("mymailbox")->get_unique<int>();
30   val1      = *received1;
31
32   received2 = sg4::Mailbox::by_name("mymailbox")->get_unique<int>();
33   val2      = *received2;
34
35   XBT_INFO("Second pair received: %ld %ld", val1, val2);
36 }
37
38 static void client(int id)
39 {
40   auto* payload1 = new int(id);
41   auto* payload2 = new int(id);
42
43   sg4::Mailbox::by_name("mymailbox")->put(payload1, 10000);
44   sg4::Mailbox::by_name("mymailbox")->put(payload2, 10000);
45 }
46
47 int main(int argc, char* argv[])
48 {
49   sg4::Engine e(&argc, argv);
50
51   e.load_platform(argv[1]);
52
53   sg4::Actor::create("server", e.host_by_name("HostA"), server);
54   sg4::Actor::create("client", e.host_by_name("HostB"), client, 1);
55   sg4::Actor::create("client", e.host_by_name("HostC"), client, 2);
56
57   e.run();
58   return 0;
59 }