X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/df5795d9b798c2f1ba70920142ee3c5d1b47253c..7ef49c428ab0209965a09a36ab28b59789aaa4b5:/examples/s4u/mutex/s4u_mutex.cpp diff --git a/examples/s4u/mutex/s4u_mutex.cpp b/examples/s4u/mutex/s4u_mutex.cpp index f1c0eb7ded..2f2afd9ccd 100644 --- a/examples/s4u/mutex/s4u_mutex.cpp +++ b/examples/s4u/mutex/s4u_mutex.cpp @@ -1,66 +1,60 @@ -/* Copyright (c) 2006-2015. The SimGrid Team. All rights reserved. */ +/* Copyright (c) 2006-2017. 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 -#include +#include /* std::mutex and std::lock_guard */ +#include "simgrid/s4u.hpp" /* All of S4U */ -#include - -#include "simgrid/s4u.h" - -#define NB_ACTOR 2 +#define NB_ACTOR 6 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_test, "a sample log category"); -static void worker(simgrid::s4u::Mutex mutex, int& result) +/* This worker uses a classical mutex */ +static void worker(simgrid::s4u::MutexPtr mutex, int& result) { - // Do the calculation - simgrid::s4u::this_actor::execute(1000); - // lock the mutex before enter in the critical section - std::lock_guard lock(mutex); - XBT_INFO("Hello s4u, I'm ready to compute"); + mutex->lock(); + XBT_INFO("Hello s4u, I'm ready to compute after a regular lock"); // And finaly add it to the results result += 1; XBT_INFO("I'm done, good bye"); + + // You have to unlock the mutex if you locked it manually. + // Beware of exceptions preventing your unlock() from being executed! + mutex->unlock(); } -static void workerLockGuard(simgrid::s4u::Mutex mutex, int& result) +static void workerLockGuard(simgrid::s4u::MutexPtr mutex, int& result) { - simgrid::s4u::this_actor::execute(1000); - // Simply use the std::lock_guard like this - std::lock_guard lock(mutex); + // It's like a lock() that would do the unlock() automatically when getting out of scope + std::lock_guard lock(*mutex); // then you are in a safe zone - XBT_INFO("Hello s4u, I'm ready to compute"); + XBT_INFO("Hello s4u, I'm ready to compute after a lock_guard"); // update the results result += 1; XBT_INFO("I'm done, good bye"); + + // Nothing specific here: the unlock will be automatic } static void master() { int result = 0; - simgrid::s4u::Mutex mutex; - simgrid::s4u::Actor workers[NB_ACTOR*2]; + simgrid::s4u::MutexPtr mutex = simgrid::s4u::Mutex::createMutex(); for (int i = 0; i < NB_ACTOR * 2 ; i++) { // To create a worker use the static method simgrid::s4u::Actor. if((i % 2) == 0 ) - workers[i] = simgrid::s4u::Actor("worker", - simgrid::s4u::Host::by_name("Jupiter"), - workerLockGuard, mutex, std::ref(result)); + simgrid::s4u::Actor::createActor("worker", simgrid::s4u::Host::by_name("Jupiter"), workerLockGuard, mutex, std::ref(result)); else - workers[i] = simgrid::s4u::Actor("worker", - simgrid::s4u::Host::by_name("Tremblay"), - worker, mutex, std::ref(result)); + simgrid::s4u::Actor::createActor("worker", simgrid::s4u::Host::by_name("Tremblay"), worker, mutex, std::ref(result)); } - simgrid::s4u::this_actor::sleep(10); + simgrid::s4u::this_actor::sleep_for(10); XBT_INFO("Results is -> %d", result); } @@ -68,7 +62,9 @@ int main(int argc, char **argv) { simgrid::s4u::Engine *e = new simgrid::s4u::Engine(&argc,argv); e->loadPlatform("../../platforms/two_hosts.xml"); - simgrid::s4u::Actor("main", simgrid::s4u::Host::by_name("Tremblay"), master); + simgrid::s4u::Actor::createActor("main", simgrid::s4u::Host::by_name("Tremblay"), master); e->run(); + + delete e; return 0; }