Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Suppressed a bit too much of codes
[simgrid.git] / examples / cpp / synchro-mutex / s4u-synchro-mutex.cpp
index 9c9f45dafd1a106395051fd34ee3f7c38dfdceb8..241322650e59948fab8feaae3c5c09171e8af3b0 100644 (file)
@@ -1,19 +1,20 @@
-/* Copyright (c) 2006-2022. The SimGrid Team. All rights reserved.          */
+/* Copyright (c) 2006-2023. 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. */
 
 #include "simgrid/s4u.hpp" /* All of S4U */
-#include "simgrid/modelchecker.h" // This example is also used to test the modelchecker on mutexes
+#include "xbt/config.hpp"
+#include <mutex> /* std::mutex and std::scoped_lock */
 
-#include <mutex> /* std::mutex and std::lock_guard */
+namespace sg4 = simgrid::s4u;
 
-int NB_ACTOR = 6;
+static simgrid::config::Flag<int> cfg_actor_count("actors", "How many pairs of actors should be started?", 6);
 
 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_test, "a sample log category");
 
 /* This worker uses a classical mutex */
-static void worker(simgrid::s4u::MutexPtr mutex, int& result)
+static void worker(sg4::MutexPtr mutex, int& result)
 {
   // lock the mutex before enter in the critical section
   mutex->lock();
@@ -28,14 +29,14 @@ static void worker(simgrid::s4u::MutexPtr mutex, int& result)
   mutex->unlock();
 }
 
-static void workerLockGuard(simgrid::s4u::MutexPtr mutex, int& result)
+static void workerScopedLock(sg4::MutexPtr mutex, int& result)
 {
-  // Simply use the std::lock_guard like this
+  // Simply use the std::scoped_lock like this
   // It's like a lock() that would do the unlock() automatically when getting out of scope
-  std::lock_guard<simgrid::s4u::Mutex> lock(*mutex);
+  const std::scoped_lock lock(*mutex);
 
   // then you are in a safe zone
-  XBT_INFO("Hello s4u, I'm ready to compute after a lock_guard");
+  XBT_INFO("Hello s4u, I'm ready to compute after a scoped_lock");
   // update the results
   result += 1;
   XBT_INFO("I'm done, good bye");
@@ -43,34 +44,24 @@ static void workerLockGuard(simgrid::s4u::MutexPtr mutex, int& result)
   // Nothing specific here: the unlock will be automatic
 }
 
-static void master()
+int main(int argc, char** argv)
 {
-  int result = 0;
-  simgrid::s4u::MutexPtr mutex = simgrid::s4u::Mutex::create();
-
-  for (int i = 0; i < NB_ACTOR * 2 ; i++) {
-    // To create a worker use the static method simgrid::s4u::Actor.
-    if((i % 2) == 0 )
-      simgrid::s4u::Actor::create("worker", simgrid::s4u::Host::by_name("Jupiter"), workerLockGuard, mutex,
-                                  std::ref(result));
-    else
-      simgrid::s4u::Actor::create("worker", simgrid::s4u::Host::by_name("Tremblay"), worker, mutex, std::ref(result));
-  }
-
-  simgrid::s4u::this_actor::sleep_for(10);
-  XBT_INFO("Results is -> %d", result);
-}
+  sg4::Engine e(&argc, argv);
+  e.load_platform(argc > 1 ? argv[1] : "../../platforms/two_hosts.xml");
 
-int main(int argc, char **argv)
-{
-  simgrid::s4u::Engine e(&argc, argv);
+  /* Create the requested amount of actors pairs. Each pair has a specific mutex and cell in `result`. */
+  std::vector<int> result(cfg_actor_count.get());
 
-  if (MC_is_active()) // Reduce the size of that test when running in the model-checker
-    NB_ACTOR = 2;
+  for (int i = 0; i < cfg_actor_count; i++) {
+    sg4::MutexPtr mutex = sg4::Mutex::create();
+    sg4::Actor::create("worker", sg4::Host::by_name("Jupiter"), workerScopedLock, mutex, std::ref(result[i]));
+    sg4::Actor::create("worker", sg4::Host::by_name("Tremblay"), worker, mutex, std::ref(result[i]));
+  }
 
-  e.load_platform("../../platforms/two_hosts.xml");
-  simgrid::s4u::Actor::create("main", e.host_by_name("Tremblay"), master);
   e.run();
 
+  for (int i = 0; i < cfg_actor_count; i++)
+    XBT_INFO("Result[%d] -> %d", i, result[i]);
+
   return 0;
 }