Logo AND Algorithmique Numérique Distribuée

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