1 /* Copyright (c) 2006-2015. The SimGrid Team. All rights reserved. */
3 /* This program is free software; you can redistribute it and/or modify it
4 * under the terms of the license (GNU LGPL) which comes with this package. */
6 #include <xbt/sysdep.h>
9 #include "simgrid/s4u.h"
13 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_test, "a sample log category");
15 // simgrid::s4u::Mutex mtx; //FIXME generate error -> You must run MSG_init before using MSG
17 //Create an actor as a c++ functor
19 simgrid::s4u::Mutex *pMtx;
22 Worker(int *res, simgrid::s4u::Mutex *mtx) : pMtx(mtx), pResults(res) {};
23 // Define the code of the actor
26 simgrid::s4u::this_actor::execute(1000);
28 // lock the mutex before enter in th critical section
30 XBT_INFO("Hello s4u, I'm ready to compute");
32 // And finaly add it to the results
34 XBT_INFO("I'm done, good bye");
36 //Then unlock the mutex, so other threads will be able to update their results
41 // This class is an example of how to use lock_guard with simgrid mutex
42 class WorkerLockGuard {
43 simgrid::s4u::Mutex *pMtx;
46 WorkerLockGuard(int *res, simgrid::s4u::Mutex *mtx) : pMtx(mtx), pResults(res) {};
49 simgrid::s4u::this_actor::execute(1000);
51 // Simply use the std::lock_guard like this
52 std::lock_guard<simgrid::s4u::Mutex> lock(*pMtx);
54 // then you are in a safe zone
55 XBT_INFO("Hello s4u, I'm ready to compute");
58 XBT_INFO("I'm done, good bye");
66 simgrid::s4u::Mutex *mtx = new simgrid::s4u::Mutex();
68 simgrid::s4u::Actor* workers[NB_ACTOR*2];
70 for (int i = 0; i < NB_ACTOR * 2 ; i++) {
71 // To create a worker use the static method simgrid::s4u::Actor.
73 workers[i] = new simgrid::s4u::Actor("worker", simgrid::s4u::Host::by_name("Jupiter"), WorkerLockGuard(res,mtx));
75 workers[i] = new simgrid::s4u::Actor("worker", simgrid::s4u::Host::by_name("Tremblay"), Worker(res,mtx));
78 for (int i = 0; i < NB_ACTOR ; i++) {
82 simgrid::s4u::this_actor::sleep(10);
83 XBT_INFO("Results is -> %d", *res);
89 int main(int argc, char **argv) {
90 simgrid::s4u::Engine *e = new simgrid::s4u::Engine(&argc,argv);
91 e->loadPlatform("../../platforms/two_hosts.xml");
92 new simgrid::s4u::Actor("main", simgrid::s4u::Host::by_name("Tremblay"), 0, MainActor());