Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
convert mc-bugged1 and mc-bugged2
[simgrid.git] / examples / s4u / mc-bugged1 / s4u-mc-bugged1.cpp
1 /* Copyright (c) 2010-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 /******************** 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 #define N 3
15
16 XBT_LOG_NEW_DEFAULT_CATEGORY(example, "this example");
17
18 static void server()
19 {
20   void* received = nullptr;
21   int count      = 0;
22   while (count < N) {
23     if (received) {
24       delete static_cast<int*>(received);
25       received = nullptr;
26     }
27     received = simgrid::s4u::Mailbox::by_name("mymailbox")->get();
28     count++;
29   }
30   int value_got = *(static_cast<int*>(received));
31   MC_assert(value_got == 3);
32
33   XBT_INFO("OK");
34 }
35
36 static void client(int id)
37 {
38   int* payload = new int();
39   *payload     = id;
40   simgrid::s4u::Mailbox::by_name("mymailbox")->put(payload, 10000);
41
42   XBT_INFO("Sent!");
43 }
44
45 int main(int argc, char* argv[])
46 {
47   simgrid::s4u::Engine e(&argc, argv);
48
49   e.load_platform(argv[1]);
50
51   simgrid::s4u::Actor::create("server", simgrid::s4u::Host::by_name("HostA"), server);
52   simgrid::s4u::Actor::create("client", simgrid::s4u::Host::by_name("HostB"), client, 1);
53   simgrid::s4u::Actor::create("client", simgrid::s4u::Host::by_name("HostC"), client, 2);
54   simgrid::s4u::Actor::create("client", simgrid::s4u::Host::by_name("HostD"), client, 3);
55
56   e.run();
57   return 0;
58 }