X-Git-Url: http://git.euphorik.ch/index.cgi?a=blobdiff_plain;f=WCudaMSE%2FTuto_Boost%2Fsrc%2Fcpp%2Fcore%2Fthread%2F03_thread_objet.cpp;fp=WCudaMSE%2FTuto_Boost%2Fsrc%2Fcpp%2Fcore%2Fthread%2F03_thread_objet.cpp;h=3575e5a9b9a94446505a0694460f5ccfd8b10187;hb=8d08c12b29c2a14684f35c023ee39e694bb80d25;hp=0000000000000000000000000000000000000000;hpb=226de81f7e1f1fbf4ac79d0d089e8a05ec7159a0;p=GPU.git diff --git a/WCudaMSE/Tuto_Boost/src/cpp/core/thread/03_thread_objet.cpp b/WCudaMSE/Tuto_Boost/src/cpp/core/thread/03_thread_objet.cpp new file mode 100755 index 0000000..3575e5a --- /dev/null +++ b/WCudaMSE/Tuto_Boost/src/cpp/core/thread/03_thread_objet.cpp @@ -0,0 +1,68 @@ +#include +#include +#include +#include "Runnable.h" + + +using std::cout; +using std::endl; + +using boost::thread; +using boost::ref; + +/*----------------------------------------------------------------------*\ + |* Declaration *| + \*---------------------------------------------------------------------*/ + +/*--------------------------------------*\ + |* Imported *| + \*-------------------------------------*/ + +/*--------------------------------------*\ + |* Public *| + \*-------------------------------------*/ + +void helloThread_Objet(void); + +/*--------------------------------------*\ + |* Private *| + \*-------------------------------------*/ + +/*----------------------------------------------------------------------*\ + |* Implementation *| + \*---------------------------------------------------------------------*/ + +/*--------------------------------------*\ + |* Public *| + \*-------------------------------------*/ + +void helloThread_Objet(void) + { + int x = 10; + int y = 100; + Runnable runnable(x, y); + + cout << "\nthread : objet : start" << endl; + + // A very important consideration when using functors with boost threads is that the thread constructor takes the functor parameter by value !! + // boost::thread threadRunnable(runnable); + + // Solution: Here runnable is passed by reference (not a copy) + thread threadRunnable(ref(runnable)); // use foncteur runnable() of Runnable + + cout << "Waiting for thread ..." << endl; + + threadRunnable.join(); + cout << "[outside thread] : " << x << " + " << y << " = " << runnable.getResult() << endl; // give bad result if runnable is passed by value to boost::thread + + cout << "thread : objet : end" << endl; + } + +/*--------------------------------------*\ + |* Private *| + \*-------------------------------------*/ + +/*----------------------------------------------------------------------*\ + |* End *| + \*---------------------------------------------------------------------*/ +