Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[s4u] Use simix simix::Mutex refcount support for s4u::Mutex
[simgrid.git] / examples / s4u / mutex / s4u_mutex.cpp
1 /* Copyright (c) 2006-2015. The SimGrid Team. All rights reserved.          */
2
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. */
5
6 #include <xbt/sysdep.h>
7 #include <mutex>
8
9 #include "simgrid/s4u.h"
10
11 #define NB_ACTOR 2
12
13 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_test, "a sample log category");
14
15 // simgrid::s4u::Mutex mtx; //FIXME generate error -> You must run MSG_init before using MSG
16
17 //Create an actor as a c++ functor
18 class Worker {
19   simgrid::s4u::Mutex mutex_;
20 int *pResults;
21 public:
22   Worker(int  *res, simgrid::s4u::Mutex mutex) :
23     mutex_(std::move(mutex)),  pResults(res) {};
24   // Define the code of the actor
25   void operator()() {
26     // Do the calculation
27     simgrid::s4u::this_actor::execute(1000);
28
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");
32
33     // And finaly add it to the results
34     *pResults += 1;
35     XBT_INFO("I'm done, good bye");
36   }
37 };
38
39 // This class is an example of how to use lock_guard with simgrid mutex
40 class WorkerLockGuard {
41   simgrid::s4u::Mutex mutex_;
42 int *pResults;
43 public:
44   WorkerLockGuard(int  *res, simgrid::s4u::Mutex mutex) :
45     mutex_(std::move(mutex)),  pResults(res) {};
46   void operator()() {
47
48     simgrid::s4u::this_actor::execute(1000);
49
50     // Simply use the std::lock_guard like this
51     std::lock_guard<simgrid::s4u::Mutex> lock(mutex_);
52
53     // then you are in a safe zone
54     XBT_INFO("Hello s4u, I'm ready to compute");
55     // update the results
56     *pResults += 1;
57     XBT_INFO("I'm done, good bye");
58   }
59 };
60
61 class MainActor {
62 public:
63   void operator()() {
64     int *res = new int;
65     simgrid::s4u::Mutex mutex;
66     *res = 0;
67     simgrid::s4u::Actor* workers[NB_ACTOR*2];
68
69     for (int i = 0; i < NB_ACTOR * 2 ; i++) {
70       // To create a worker use the static method simgrid::s4u::Actor.
71       if((i % 2) == 0 )
72         workers[i] = new simgrid::s4u::Actor("worker",
73           simgrid::s4u::Host::by_name("Jupiter"),
74           WorkerLockGuard(res, mutex));
75       else
76         workers[i] = new simgrid::s4u::Actor("worker",
77           simgrid::s4u::Host::by_name("Tremblay"),
78           Worker(res,mutex));
79     }
80
81     for (int i = 0; i < NB_ACTOR ; i++) {
82       delete workers[i];
83     }
84
85     simgrid::s4u::this_actor::sleep(10);
86     XBT_INFO("Results is -> %d", *res);
87   }
88 };
89
90
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());
95   e->run();
96   return 0;
97 }