Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines.
[simgrid.git] / examples / s4u / maestro-set / s4u-maestro-set.cpp
index 1088559..3f5e8d8 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (c) 2007-2019. The SimGrid Team. All rights reserved.          */
+/* Copyright (c) 2007-2021. The SimGrid Team. All rights reserved.          */
 
 /* This program is free software; you can redistribute it and/or modify it
  * under the terms of the license (GNU LGPL) which comes with this package. */
@@ -6,7 +6,7 @@
 /** @addtogroup S4U_examples
  *
  *  - <b>maestro-set/maestro-set.cpp: Switch the system thread hosting our maestro</b>.
- *    That's a very advanced example in which we move the maestro thread to another process.
+ *    That's a very advanced example in which we move the maestro context to another system thread.
  *    Not many users need it (maybe only one, actually), but this example is also a regression test.
  *
  *    This example is in C++ because we use C++11 threads to ensure that the feature is working as
@@ -22,7 +22,7 @@
 
 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_test, "Messages specific for this s4u example");
 
-std::thread::id root_id;
+const std::thread::id root_id = std::this_thread::get_id();
 
 static void ensure_root_tid()
 {
@@ -40,17 +40,16 @@ static void ensure_other_tid()
 static void sender()
 {
   ensure_root_tid();
-  std::string* payload = new std::string("some message");
-  simgrid::s4u::Mailbox::by_name("some mailbox")->put((void*)payload, 10e8);
+  auto* payload = new std::string("some message");
+  simgrid::s4u::Mailbox::by_name("some mailbox")->put(payload, 10e8);
 }
 
 static void receiver()
 {
   ensure_other_tid();
 
-  std::string* payload = static_cast<std::string*>(simgrid::s4u::Mailbox::by_name("some mailbox")->get());
+  simgrid::s4u::Mailbox::by_name("some mailbox")->get_unique<std::string>();
   XBT_INFO("Task received");
-  delete payload;
 }
 
 static void maestro(void* /* data */)
@@ -63,9 +62,10 @@ static void maestro(void* /* data */)
 /** Main function */
 int main(int argc, char* argv[])
 {
-  root_id = std::this_thread::get_id();
+  /* Specify which code should be executed by maestro on another thread, once this current thread is affected to an
+   * actor by the subsequent sg_actor_attach(). This must be done before the creation of the engine. */
+  SIMIX_set_maestro(maestro, nullptr);
 
-  SIMIX_set_maestro(maestro, NULL);
   simgrid::s4u::Engine e(&argc, argv);
 
   if (argc != 2) {
@@ -75,16 +75,16 @@ int main(int argc, char* argv[])
 
   e.load_platform(argv[1]);
 
-  /* Become one of the simulated process.
-   *
-   * This must be done after the creation of the platform because we are depending attaching to a host.*/
+  /* Become one of the simulated process (must be done after the platform creation, or the host won't exist). */
   sg_actor_attach("sender", nullptr, simgrid::s4u::Host::by_name("Tremblay"), nullptr);
-  ensure_root_tid();
 
-  // Execute the sender code:
+  ensure_root_tid(); // Only useful in this test: we ensure that simgrid is not broken and that this code is executed in
+                     // the correct system thread
+
+  // Execute the sender code. The root thread was actually turned into a regular actor
   sender();
 
-  sg_actor_detach(); // Become root thread again
+  sg_actor_detach(); // The root thread becomes maestro again (as proved by the output)
   XBT_INFO("Detached");
   ensure_root_tid();