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 mutex_;
22 Worker(int *res, simgrid::s4u::Mutex mutex) :
23 mutex_(std::move(mutex)), pResults(res) {};
24 // Define the code of the actor
27 simgrid::s4u::this_actor::execute(1000);
29 // lock the mutex before enter in the critical section
30 std::lock_guard<simgrid::s4u::Mutex> lock(mutex_);
31 XBT_INFO("Hello s4u, I'm ready to compute");
33 // And finaly add it to the results
35 XBT_INFO("I'm done, good bye");
39 // This class is an example of how to use lock_guard with simgrid mutex
40 class WorkerLockGuard {
41 simgrid::s4u::Mutex mutex_;
44 WorkerLockGuard(int *res, simgrid::s4u::Mutex mutex) :
45 mutex_(std::move(mutex)), pResults(res) {};
48 simgrid::s4u::this_actor::execute(1000);
50 // Simply use the std::lock_guard like this
51 std::lock_guard<simgrid::s4u::Mutex> lock(mutex_);
53 // then you are in a safe zone
54 XBT_INFO("Hello s4u, I'm ready to compute");
57 XBT_INFO("I'm done, good bye");
65 simgrid::s4u::Mutex mutex;
67 simgrid::s4u::Actor* workers[NB_ACTOR*2];
69 for (int i = 0; i < NB_ACTOR * 2 ; i++) {
70 // To create a worker use the static method simgrid::s4u::Actor.
72 workers[i] = new simgrid::s4u::Actor("worker",
73 simgrid::s4u::Host::by_name("Jupiter"),
74 WorkerLockGuard(res, mutex));
76 workers[i] = new simgrid::s4u::Actor("worker",
77 simgrid::s4u::Host::by_name("Tremblay"),
81 for (int i = 0; i < NB_ACTOR ; i++) {
85 simgrid::s4u::this_actor::sleep(10);
86 XBT_INFO("Results is -> %d", *res);
91 int main(int argc, char **argv) {
92 simgrid::s4u::Engine *e = new simgrid::s4u::Engine(&argc,argv);
93 e->loadPlatform("../../platforms/two_hosts.xml");
94 new simgrid::s4u::Actor("main", simgrid::s4u::Host::by_name("Tremblay"), 0, MainActor());