Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[simix] Refcounting with SIMIX_{process,mutex}_{ref,unref}()
[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 *results_;
21 public:
22   Worker(int  *res, simgrid::s4u::Mutex mutex) :
23     mutex_(std::move(mutex)),  results_(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     *results_ += 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 *results_;
43 public:
44   WorkerLockGuard(int  *res, simgrid::s4u::Mutex mutex) :
45     mutex_(std::move(mutex)),  results_(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     *results_ += 1;
57     XBT_INFO("I'm done, good bye");
58   }
59 };
60
61 class MainActor {
62 public:
63   void operator()() {
64     int res = 0;
65     simgrid::s4u::Mutex mutex;
66     simgrid::s4u::Actor* workers[NB_ACTOR*2];
67
68     for (int i = 0; i < NB_ACTOR * 2 ; i++) {
69       // To create a worker use the static method simgrid::s4u::Actor.
70       if((i % 2) == 0 )
71         workers[i] = new simgrid::s4u::Actor("worker",
72           simgrid::s4u::Host::by_name("Jupiter"),
73           WorkerLockGuard(&res, mutex));
74       else
75         workers[i] = new simgrid::s4u::Actor("worker",
76           simgrid::s4u::Host::by_name("Tremblay"),
77           Worker(&res, mutex));
78     }
79
80     for (int i = 0; i < NB_ACTOR ; i++) {
81       delete workers[i];
82     }
83
84     simgrid::s4u::this_actor::sleep(10);
85     XBT_INFO("Results is -> %d", res);
86   }
87 };
88
89
90 int main(int argc, char **argv) {
91   simgrid::s4u::Engine *e = new simgrid::s4u::Engine(&argc,argv);
92   e->loadPlatform("../../platforms/two_hosts.xml");
93   new simgrid::s4u::Actor("main", simgrid::s4u::Host::by_name("Tremblay"), 0, MainActor());
94   e->run();
95   return 0;
96 }