A
lgorithmique
N
umérique
D
istribuée
Public GIT Repository
projects
/
simgrid.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Change an example to take its platform file from the command line if provided
[simgrid.git]
/
examples
/
cpp
/
synchro-mutex
/
s4u-synchro-mutex.cpp
diff --git
a/examples/cpp/synchro-mutex/s4u-synchro-mutex.cpp
b/examples/cpp/synchro-mutex/s4u-synchro-mutex.cpp
index
52e2595
..
2413226
100644
(file)
--- a/
examples/cpp/synchro-mutex/s4u-synchro-mutex.cpp
+++ b/
examples/cpp/synchro-mutex/s4u-synchro-mutex.cpp
@@
-1,11
+1,11
@@
-/* Copyright (c) 2006-202
2
. The SimGrid Team. All rights reserved. */
+/* Copyright (c) 2006-202
3
. 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 "xbt/config.hpp"
/* 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 "xbt/config.hpp"
-#include <mutex> /* std::mutex and std::
lock_guard
*/
+#include <mutex> /* std::mutex and std::
scoped_lock
*/
namespace sg4 = simgrid::s4u;
namespace sg4 = simgrid::s4u;
@@
-29,14
+29,14
@@
static void worker(sg4::MutexPtr mutex, int& result)
mutex->unlock();
}
mutex->unlock();
}
-static void worker
LockGuard
(sg4::MutexPtr mutex, int& result)
+static void worker
ScopedLock
(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
// It's like a lock() that would do the unlock() automatically when getting out of scope
-
std::lock_guard<sg4::Mutex>
lock(*mutex);
+
const std::scoped_lock
lock(*mutex);
// then you are in a safe zone
// 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");
// update the results
result += 1;
XBT_INFO("I'm done, good bye");
@@
-44,29
+44,24
@@
static void workerLockGuard(sg4::MutexPtr mutex, int& result)
// Nothing specific here: the unlock will be automatic
}
// Nothing specific here: the unlock will be automatic
}
-
static void master(
)
+
int main(int argc, char** argv
)
{
{
+ sg4::Engine e(&argc, argv);
+ e.load_platform(argc > 1 ? argv[1] : "../../platforms/two_hosts.xml");
+
/* Create the requested amount of actors pairs. Each pair has a specific mutex and cell in `result`. */
/* Create the requested amount of actors pairs. Each pair has a specific mutex and cell in `result`. */
-
int result[cfg_actor_count.get()]
;
+
std::vector<int> result(cfg_actor_count.get())
;
for (int i = 0; i < cfg_actor_count; i++) {
for (int i = 0; i < cfg_actor_count; i++) {
- result[i] = 0;
sg4::MutexPtr mutex = sg4::Mutex::create();
sg4::MutexPtr mutex = sg4::Mutex::create();
- sg4::Actor::create("worker", sg4::Host::by_name("Jupiter"), worker
LockGuard
, mutex, std::ref(result[i]));
+ sg4::Actor::create("worker", sg4::Host::by_name("Jupiter"), worker
ScopedLock
, mutex, std::ref(result[i]));
sg4::Actor::create("worker", sg4::Host::by_name("Tremblay"), worker, mutex, std::ref(result[i]));
}
sg4::Actor::create("worker", sg4::Host::by_name("Tremblay"), worker, mutex, std::ref(result[i]));
}
- sg4::this_actor::sleep_for(10);
+ e.run();
+
for (int i = 0; i < cfg_actor_count; i++)
XBT_INFO("Result[%d] -> %d", i, result[i]);
for (int i = 0; i < cfg_actor_count; i++)
XBT_INFO("Result[%d] -> %d", i, result[i]);
-}
-
-int main(int argc, char **argv)
-{
- sg4::Engine e(&argc, argv);
- e.load_platform("../../platforms/two_hosts.xml");
- sg4::Actor::create("main", e.host_by_name("Tremblay"), master);
- e.run();
return 0;
}
return 0;
}