1 /* Copyright (c) 2017. The SimGrid Team. All rights reserved. */
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. */
7 #include <simgrid/s4u.hpp>
13 XBT_LOG_NEW_DEFAULT_CATEGORY(mwe, "Minimum Working Example");
15 static void receiver()
17 simgrid::s4u::MailboxPtr mymailbox = simgrid::s4u::Mailbox::byName("receiver_mailbox");
19 std::vector<simgrid::s4u::CommPtr> pending_comms;
21 XBT_INFO("Placing %d asynchronous recv requests", NUM_COMMS);
23 for (int i = 0; i < NUM_COMMS; i++) {
24 simgrid::s4u::CommPtr comm = simgrid::s4u::Comm::recv_async(mymailbox, &data);
25 pending_comms.push_back(comm);
28 for (int i = 0; i < NUM_COMMS; i++) {
29 XBT_INFO("Sleeping for 3 seconds (for the %dth time)...", i + 1);
30 simgrid::s4u::this_actor::sleep_for(3.0);
31 XBT_INFO("Calling wait_any() for %zu pending comms", pending_comms.size());
32 std::vector<simgrid::s4u::CommPtr>::iterator ret_it =
33 simgrid::s4u::Comm::wait_any(pending_comms.begin(), pending_comms.end());
34 XBT_INFO("Counting the number of completed comms...");
37 for (; ret_it != pending_comms.end(); count++, ret_it++)
40 XBT_INFO("wait_any() replied that %d comms have completed", count);
41 // xbt_assert(count == 1, "wait_any() replied that %d comms have completed, which is broken!", count);
47 simgrid::s4u::MailboxPtr theirmailbox = simgrid::s4u::Mailbox::byName("receiver_mailbox");
49 void* data = (void*)"data";
51 for (int i = 0; i < NUM_COMMS; i++) {
52 XBT_INFO("Sending a message to the receiver");
53 simgrid::s4u::this_actor::send(theirmailbox, &data, 4);
54 XBT_INFO("Sleeping for 1000 seconds");
55 simgrid::s4u::this_actor::sleep_for(1000.0);
59 int main(int argc, char** argv)
62 simgrid::s4u::Engine* engine = new simgrid::s4u::Engine(&argc, argv);
64 xbt_assert(argc >= 2, "Usage: %s <xml platform file>", argv[0]);
66 engine->loadPlatform(argv[1]);
67 simgrid::s4u::Host** hosts = sg_host_list();
68 simgrid::s4u::Actor::createActor("Receiver", hosts[0], receiver);
69 simgrid::s4u::Actor::createActor("Sender", hosts[1], sender);
72 simgrid::s4u::Engine::instance()->run();