Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of https://framagit.org/simgrid/simgrid
[simgrid.git] / teshsuite / s4u / wait-any-for / wait-any-for.cpp
1 #include <cstdlib>
2 #include <iostream>
3 #include <string>
4 #include <simgrid/s4u.hpp>
5
6 XBT_LOG_NEW_DEFAULT_CATEGORY(meh, "meh");
7
8 static void worker()
9 {
10   auto mbox = simgrid::s4u::Mailbox::by_name("meh");
11   int input1 = 42;
12   int input2 = 51;
13
14   XBT_INFO("Sending and receiving %d and %d asynchronously", input1, input2);
15
16   auto put1 = mbox->put_async(&input1, 1000 * 1000 * 500);
17   auto put2 = mbox->put_async(&input2, 1000 * 1000 * 1000);
18
19   int* out1;
20   auto get1 = mbox->get_async<int>(&out1);
21
22   int* out2;
23   auto get2 = mbox->get_async<int>(&out2);
24
25   XBT_INFO("All comms have started");
26   std::vector<simgrid::s4u::CommPtr> comms = {put1, put2, get1, get2};
27
28   while (!comms.empty()) {
29     int index = simgrid::s4u::Comm::wait_any_for(&comms, 0.5);
30     if (index < 0)
31       XBT_INFO("wait_any_for: Timeout reached");
32     else {
33       XBT_INFO("wait_any_for: A comm finished (index=%d, #comms=%zu)", index, comms.size());
34       comms.erase(comms.begin() + index);
35     }
36   }
37
38   XBT_INFO("All comms have finished");
39   XBT_INFO("Got %d and %d", *out1, *out2);
40 }
41
42 int main(int argc, char* argv[])
43
44 {
45   simgrid::s4u::Engine e(&argc, argv);
46   e.load_platform(argv[1]);
47   simgrid::s4u::Actor::create("worker", simgrid::s4u::Host::by_name("Tremblay"), worker);
48   e.run();
49   return 0;
50 }