Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[s4u] add 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 *pMtx;
20 int *pResults;
21 public:
22   Worker(int  *res, simgrid::s4u::Mutex *mtx) : pMtx(mtx),  pResults(res) {};
23   // Define the code of the actor
24   void operator()() {
25     // Do the calculation
26     simgrid::s4u::this_actor::execute(1000);
27
28     // lock the mutex before enter in th critical section
29     pMtx->lock();
30     XBT_INFO("Hello s4u, I'm ready to compute");
31
32     // And finaly add it to the results
33     *pResults += 1;
34     XBT_INFO("I'm done, good bye");
35
36     //Then unlock the mutex, so other threads will be able to update their results
37     pMtx->unlock();
38   }
39 };
40
41 // This class is an example of how to use lock_guard with simgrid mutex
42 class WorkerLockGuard {
43 simgrid::s4u::Mutex *pMtx;
44 int *pResults;
45 public:
46   WorkerLockGuard(int  *res, simgrid::s4u::Mutex *mtx) : pMtx(mtx),  pResults(res) {};
47   void operator()() {
48
49     simgrid::s4u::this_actor::execute(1000);
50
51     // Simply use the std::lock_guard like this
52     std::lock_guard<simgrid::s4u::Mutex> lock(*pMtx);
53
54     // then you are in a safe zone
55     XBT_INFO("Hello s4u, I'm ready to compute");
56     // update the results
57     *pResults += 1;
58     XBT_INFO("I'm done, good bye");
59   }
60 };
61
62 class MainActor {
63 public:
64   void operator()() {
65     int *res = new int;
66     simgrid::s4u::Mutex *mtx = new simgrid::s4u::Mutex();
67     *res = 0;
68     simgrid::s4u::Actor* workers[NB_ACTOR*2];
69
70     for (int i = 0; i < NB_ACTOR * 2 ; i++) {
71       // To create a worker use the static method simgrid::s4u::Actor.
72       if((i % 2) == 0 )
73         workers[i] = new simgrid::s4u::Actor("worker", simgrid::s4u::Host::by_name("Jupiter"), WorkerLockGuard(res,mtx));
74       else
75         workers[i] = new simgrid::s4u::Actor("worker", simgrid::s4u::Host::by_name("Tremblay"), Worker(res,mtx));
76     }
77
78     for (int i = 0; i < NB_ACTOR ; i++) {
79       delete workers[i];
80     }
81
82     simgrid::s4u::this_actor::sleep(10);
83     XBT_INFO("Results is -> %d", *res);
84     delete mtx;
85   }
86 };
87
88
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());
93   e->run();
94   return 0;
95 }