Ajout de l'ensemble du workspace.
[GPU.git] / WCudaMSE / Tuto_Boost / src / cpp / core / thread / 03_thread_objet.cpp
1 #include <iostream>
2 #include <boost/thread.hpp>
3 #include <boost/date_time.hpp>
4 #include "Runnable.h"
5
6
7 using std::cout;
8 using std::endl;
9
10 using boost::thread;
11 using boost::ref;
12
13 /*----------------------------------------------------------------------*\
14 |* Declaration *|
15 \*---------------------------------------------------------------------*/
16
17 /*--------------------------------------*\
18 |* Imported *|
19 \*-------------------------------------*/
20
21 /*--------------------------------------*\
22 |* Public *|
23 \*-------------------------------------*/
24
25 void helloThread_Objet(void);
26
27 /*--------------------------------------*\
28 |* Private *|
29 \*-------------------------------------*/
30
31 /*----------------------------------------------------------------------*\
32 |* Implementation *|
33 \*---------------------------------------------------------------------*/
34
35 /*--------------------------------------*\
36 |* Public *|
37 \*-------------------------------------*/
38
39 void helloThread_Objet(void)
40 {
41 int x = 10;
42 int y = 100;
43 Runnable runnable(x, y);
44
45 cout << "\nthread : objet : start" << endl;
46
47 // A very important consideration when using functors with boost threads is that the thread constructor takes the functor parameter by value !!
48 // boost::thread threadRunnable(runnable);
49
50 // Solution: Here runnable is passed by reference (not a copy)
51 thread threadRunnable(ref(runnable)); // use foncteur runnable() of Runnable
52
53 cout << "Waiting for thread ..." << endl;
54
55 threadRunnable.join();
56 cout << "[outside thread] : " << x << " + " << y << " = " << runnable.getResult() << endl; // give bad result if runnable is passed by value to boost::thread
57
58 cout << "thread : objet : end" << endl;
59 }
60
61 /*--------------------------------------*\
62 |* Private *|
63 \*-------------------------------------*/
64
65 /*----------------------------------------------------------------------*\
66 |* End *|
67 \*---------------------------------------------------------------------*/
68