X-Git-Url: http://bilbo.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/5ed37babb2fa9097abe82df299c0aa259ed84d5a..aa4e139cb5c4ec10b125ab8739d3696b891effb0:/examples/cpp/network-nonlinear/s4u-network-nonlinear.cpp diff --git a/examples/cpp/network-nonlinear/s4u-network-nonlinear.cpp b/examples/cpp/network-nonlinear/s4u-network-nonlinear.cpp index 6384e76ddb..9bfce5b48c 100644 --- a/examples/cpp/network-nonlinear/s4u-network-nonlinear.cpp +++ b/examples/cpp/network-nonlinear/s4u-network-nonlinear.cpp @@ -23,10 +23,10 @@ public: void operator()() const { // sphinx-doc: init-begin (this line helps the doc to build; ignore it) - /* Vector in which we store all ongoing communications */ - std::vector pending_comms; + /* ActivitySet in which we store all ongoing communications */ + sg4::ActivitySet pending_comms; - /* Make a vector of the mailboxes to use */ + /* Mailbox to use */ sg4::Mailbox* mbox = sg4::Mailbox::by_name("receiver"); // sphinx-doc: init-end @@ -41,13 +41,13 @@ public: /* Create a communication representing the ongoing communication, and store it in pending_comms */ sg4::CommPtr comm = mbox->put_async(payload, size); - pending_comms.push_back(comm); + pending_comms.push(comm); } XBT_INFO("Done dispatching all messages"); /* Now that all message exchanges were initiated, wait for their completion in one single call */ - sg4::Comm::wait_all(pending_comms); + pending_comms.wait_all(); // sphinx-doc: put-end XBT_INFO("Goodbye now!"); @@ -63,23 +63,28 @@ public: explicit Receiver(int count) : messages_count(count) { mbox = sg4::Mailbox::by_name("receiver"); } void operator()() { - /* Vector in which we store all incoming msgs */ - std::vector> pending_msgs; - std::vector pending_comms; + /* Where we store all incoming msgs */ + std::unordered_map pending_msgs; + sg4::ActivitySet pending_comms; XBT_INFO("Wait for %d messages asynchronously", messages_count); for (int i = 0; i < messages_count; i++) { - pending_msgs.push_back(std::make_unique()); - pending_comms.emplace_back(mbox->get_async(pending_msgs[i].get())); + std::string* msg; + auto comm = mbox->get_async(&msg); + pending_comms.push(comm); + pending_msgs.insert({comm, &msg}); } + while (not pending_comms.empty()) { - ssize_t index = sg4::Comm::wait_any(pending_comms); - std::string* msg = *pending_msgs[index]; - XBT_INFO("I got '%s'.", msg->c_str()); - /* cleanup memory and remove from vectors */ - delete msg; - pending_comms.erase(pending_comms.begin() + index); - pending_msgs.erase(pending_msgs.begin() + index); + auto completed_one = pending_comms.wait_any(); + if (completed_one != nullptr){ + auto comm = boost::dynamic_pointer_cast(completed_one); + std::string *msg = std::move(*pending_msgs[comm]); + XBT_INFO("I got '%s'.", msg->c_str()); + /* cleanup memory and remove from map */ + delete msg; + pending_msgs.erase(comm); + } } } };