Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Convert 2 examples from Activity::wait_any to ActivitySet
[simgrid.git] / examples / cpp / activityset-testany / s4u-activityset-testany.cpp
1 /* Copyright (c) 2010-2023. 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 "simgrid/s4u.hpp"
7 #include <cstdlib>
8 #include <iostream>
9 #include <string>
10 namespace sg4 = simgrid::s4u;
11
12 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_activity_testany, "Messages specific for this s4u example");
13
14 static void bob()
15 {
16   sg4::Mailbox* mbox    = sg4::Mailbox::by_name("mbox");
17   const sg4::Disk* disk = sg4::Host::current()->get_disks().front();
18   std::string* payload;
19
20   XBT_INFO("Create my asynchronous activities");
21   auto exec = sg4::this_actor::exec_async(5e9);
22   auto comm = mbox->get_async(&payload);
23   auto io   = disk->read_async(3e8);
24
25   sg4::ActivitySet pending_activities;
26   pending_activities.push(exec);
27   pending_activities.push(comm);
28   pending_activities.push(io);
29
30   XBT_INFO("Sleep_for a while");
31   sg4::this_actor::sleep_for(1);
32
33   XBT_INFO("Test for completed activities");
34   while (not pending_activities.empty()) {
35     auto completed_one = pending_activities.test_any();
36     if (completed_one != nullptr) {
37       if (boost::dynamic_pointer_cast<sg4::Comm>(completed_one))
38         XBT_INFO("Completed a Comm");
39       if (boost::dynamic_pointer_cast<sg4::Exec>(completed_one))
40         XBT_INFO("Completed an Exec");
41       if (boost::dynamic_pointer_cast<sg4::Io>(completed_one))
42         XBT_INFO("Completed an I/O");
43     } else {
44       XBT_INFO("Nothing matches, test again in 0.5s");
45       sg4::this_actor::sleep_for(.5);
46     }
47   }
48   XBT_INFO("Last activity is complete");
49   delete payload;
50 }
51
52 static void alice()
53 {
54   auto* payload = new std::string("Message");
55   XBT_INFO("Send '%s'", payload->c_str());
56   sg4::Mailbox::by_name("mbox")->put(payload, 6e8);
57 }
58
59 int main(int argc, char* argv[])
60 {
61   sg4::Engine e(&argc, argv);
62
63   e.load_platform(argv[1]);
64
65   sg4::Actor::create("bob", e.host_by_name("bob"), bob);
66   sg4::Actor::create("alice", e.host_by_name("alice"), alice);
67
68   e.run();
69
70   return 0;
71 }