1 /* Copyright (c) 2010-2022. 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. */
6 /* This example shows how to use simgrid::s4u::Activity::wait_until() and
7 * simgrid::s4u::Activity::wait_for() on a given communication.
9 * It is very similar to the comm-wait example, but the sender initially
10 * does some waits that are too short before doing an infinite wait.
13 #include "simgrid/s4u.hpp"
17 namespace sg4 = simgrid::s4u;
19 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_comm_waituntil, "Messages specific for this s4u example");
21 static void sender(int messages_count, size_t payload_size)
23 std::vector<sg4::CommPtr> pending_comms;
24 sg4::Mailbox* mbox = sg4::Mailbox::by_name("receiver-0");
26 /* Start dispatching all messages to the receiver */
27 for (int i = 0; i < messages_count; i++) {
28 std::string message = std::string("Message ") + std::to_string(i);
29 auto* payload = new std::string(message); // copy the data we send:
31 // 'msgName' is not a stable storage location
32 XBT_INFO("Send '%s' to '%s'", message.c_str(), mbox->get_cname());
33 /* Create a communication representing the ongoing communication */
34 sg4::CommPtr comm = mbox->put_async(payload, payload_size);
35 /* Add this comm to the vector of all known comms */
36 pending_comms.push_back(comm);
39 /* Start the finalize signal to the receiver*/
40 auto* payload = new std::string("finalize"); // Make a copy of the data we will send
41 sg4::CommPtr final_comm = mbox->put_async(payload, 0);
42 pending_comms.push_back(final_comm);
43 XBT_INFO("Send 'finalize' to 'receiver-0'");
45 XBT_INFO("Done dispatching all messages");
47 /* Now that all message exchanges were initiated, wait for their completion, in order of creation. */
48 while (not pending_comms.empty()) {
49 sg4::CommPtr comm = pending_comms.back();
51 pending_comms.pop_back(); // remove it from the list
54 XBT_INFO("Goodbye now!");
57 static void receiver()
59 sg4::Mailbox* mbox = sg4::Mailbox::by_name("receiver-0");
61 XBT_INFO("Wait for my first message");
62 for (bool cont = true; cont;) {
63 auto received = mbox->get_unique<std::string>();
64 XBT_INFO("I got a '%s'.", received->c_str());
65 if (*received == "finalize")
66 cont = false; // If it's a finalize message, we're done.
70 int main(int argc, char* argv[])
72 sg4::Engine e(&argc, argv);
74 e.load_platform(argv[1]);
76 sg4::Actor::create("sender", e.host_by_name("Tremblay"), sender, 3, 5e7);
77 sg4::Actor::create("receiver", e.host_by_name("Ruby"), receiver);