Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
add an example for condition variables
authorMartin Quinson <martin.quinson@ens-rennes.fr>
Sat, 4 Jan 2020 17:05:08 +0000 (18:05 +0100)
committerMartin Quinson <martin.quinson@ens-rennes.fr>
Sat, 4 Jan 2020 18:18:56 +0000 (19:18 +0100)
examples/README.rst
examples/s4u/CMakeLists.txt
examples/s4u/synchro-condition-variable/s4u-synchro-condition-variable.cpp [new file with mode: 0644]
examples/s4u/synchro-condition-variable/s4u-synchro-condition-variable.tesh [new file with mode: 0644]

index 7fe1a36..1fb1eed 100644 (file)
@@ -407,19 +407,26 @@ result in short reads and short write, as in reality.
 Classical synchronization objects
 ---------------------------------
 
- - **Mutex:**
-   Shows how to use simgrid::s4u::Mutex synchronization objects.
+ - **Barrier:**
+   Shows how to use simgrid::s4u::Barrier synchronization objects.
 
    .. tabs::
 
-      .. example-tab:: examples/s4u/synchro-mutex/s4u-synchro-mutex.cpp
+      .. example-tab:: examples/s4u/synchro-barrier/s4u-synchro-barrier.cpp
 
- - **Barrier:**
-   Shows how to use simgrid::s4u::Barrier synchronization objects.
+ - **Condition variable:**
+   Shows how to use simgrid::s4u::ConditionVariable synchronization objects.
 
    .. tabs::
 
-      .. example-tab:: examples/s4u/synchro-barrier/s4u-synchro-barrier.cpp
+      .. example-tab:: examples/s4u/synchro-condition-variable/s4u-synchro-condition-variable.cpp
+
+ - **Mutex:**
+   Shows how to use simgrid::s4u::Mutex synchronization objects.
+
+   .. tabs::
+
+      .. example-tab:: examples/s4u/synchro-mutex/s4u-synchro-mutex.cpp
 
  - **Semaphore:**
    Shows how to use simgrid::s4u::Semaphore synchronization objects.
index 1130d4d..e3d0199 100644 (file)
@@ -15,7 +15,7 @@ foreach (example actor-create actor-daemon actor-exiting actor-join actor-kill
                  plugin-hostload
                  replay-comm replay-io
                  routing-get-clusters
-                 synchro-barrier synchro-mutex synchro-semaphore)
+                 synchro-barrier synchro-condition-variable synchro-mutex synchro-semaphore)
   add_executable       (s4u-${example} EXCLUDE_FROM_ALL ${example}/s4u-${example}.cpp)
   add_dependencies     (tests s4u-${example})
   target_link_libraries(s4u-${example} simgrid)
diff --git a/examples/s4u/synchro-condition-variable/s4u-synchro-condition-variable.cpp b/examples/s4u/synchro-condition-variable/s4u-synchro-condition-variable.cpp
new file mode 100644 (file)
index 0000000..1d828cf
--- /dev/null
@@ -0,0 +1,52 @@
+/* Copyright (c) 2006-2020. 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 <mutex>           /* std::mutex and std::lock_guard */
+#include <simgrid/s4u.hpp> /* All of S4U */
+
+XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_test, "a sample log category");
+
+std::string data;
+bool done = false;
+
+static void worker_fun(simgrid::s4u::ConditionVariablePtr cv, simgrid::s4u::MutexPtr mutex)
+{
+  std::unique_lock<simgrid::s4u::Mutex> lock(*mutex);
+
+  XBT_INFO("Start processing data which is '%s'.", data.c_str());
+  data += std::string(" after processing");
+
+  // Send data back to main()
+  XBT_INFO("Signal to master that the data processing is completed, and exit.");
+
+  done = true;
+  cv->notify_one();
+}
+
+static void master_fun()
+{
+  auto mutex  = simgrid::s4u::Mutex::create();
+  auto cv     = simgrid::s4u::ConditionVariable::create();
+  data        = std::string("Example data");
+  auto worker = simgrid::s4u::Actor::create("worker", simgrid::s4u::Host::by_name("Jupiter"), worker_fun, cv, mutex);
+
+  { // wait for the worker
+    std::unique_lock<simgrid::s4u::Mutex> lock(*mutex);
+    cv->wait(lock, []() { return done; });
+  }
+  XBT_INFO("data is now '%s'.", data.c_str());
+
+  worker->join();
+}
+
+int main(int argc, char** argv)
+{
+  simgrid::s4u::Engine e(&argc, argv);
+  e.load_platform("../../platforms/two_hosts.xml");
+  simgrid::s4u::Actor::create("main", simgrid::s4u::Host::by_name("Tremblay"), master_fun);
+  e.run();
+
+  return 0;
+}
diff --git a/examples/s4u/synchro-condition-variable/s4u-synchro-condition-variable.tesh b/examples/s4u/synchro-condition-variable/s4u-synchro-condition-variable.tesh
new file mode 100644 (file)
index 0000000..3b54bf0
--- /dev/null
@@ -0,0 +1,6 @@
+#!/usr/bin/env tesh
+
+$ ${bindir:=.}/s4u-synchro-condition-variable
+> [Jupiter:worker:(2) 0.000000] [s4u_test/INFO] Start processing data which is 'Example data'.
+> [Jupiter:worker:(2) 0.000000] [s4u_test/INFO] Signal to master that the data processing is completed, and exit.
+> [Tremblay:main:(1) 0.000000] [s4u_test/INFO] data is now 'Example data after processing'.