From 600b9466b4a5bf5bc0d966c0b9d1ae9227316584 Mon Sep 17 00:00:00 2001 From: Fred Suter Date: Mon, 17 Jul 2023 16:36:39 -0400 Subject: [PATCH] use shared pointers in these examples --- .../s4u-network-nonlinear.cpp | 11 +++---- .../s4u-platform-comm-serialize.cpp | 29 +++++++++++-------- 2 files changed, 23 insertions(+), 17 deletions(-) diff --git a/examples/cpp/network-nonlinear/s4u-network-nonlinear.cpp b/examples/cpp/network-nonlinear/s4u-network-nonlinear.cpp index 9bfce5b48c..c403d2ba74 100644 --- a/examples/cpp/network-nonlinear/s4u-network-nonlinear.cpp +++ b/examples/cpp/network-nonlinear/s4u-network-nonlinear.cpp @@ -8,6 +8,7 @@ */ #include +#include namespace sg4 = simgrid::s4u; @@ -64,22 +65,22 @@ public: void operator()() { /* Where we store all incoming msgs */ - std::unordered_map pending_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++) { - std::string* msg; - auto comm = mbox->get_async(&msg); + std::shared_ptr msg =std::make_shared(); + auto comm = mbox->get_async(msg.get()); pending_comms.push(comm); - pending_msgs.insert({comm, &msg}); + pending_msgs.insert({comm, msg}); } while (not pending_comms.empty()) { 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]); + auto msg = *pending_msgs[comm]; XBT_INFO("I got '%s'.", msg->c_str()); /* cleanup memory and remove from map */ delete msg; diff --git a/examples/cpp/platform-comm-serialize/s4u-platform-comm-serialize.cpp b/examples/cpp/platform-comm-serialize/s4u-platform-comm-serialize.cpp index cbff56d166..3335631aaa 100644 --- a/examples/cpp/platform-comm-serialize/s4u-platform-comm-serialize.cpp +++ b/examples/cpp/platform-comm-serialize/s4u-platform-comm-serialize.cpp @@ -66,23 +66,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::shared_ptr msg =std::make_shared(); + auto comm = mbox->get_async(msg.get()); + 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); + auto msg = *pending_msgs[comm]; + XBT_INFO("I got '%s'.", msg->c_str()); + /* cleanup memory and remove from map */ + delete msg; + pending_msgs.erase(comm); + } } } }; -- 2.20.1