thread.cpp
Go to the documentation of this file.
00001 
00008 /*****************************************************************************
00009 ** Platform Check
00010 *****************************************************************************/
00011 
00012 #include <ecl/config.hpp>
00013 
00014 /*****************************************************************************
00015 ** Includes
00016 *****************************************************************************/
00017 
00018 #include <iostream>
00019 #include "../../include/ecl/threads/thread.hpp"
00020 #include "../../../ecl_time/include/ecl/time/sleep.hpp"
00021 
00022 class ThreadMemberFunctions {
00023 public:
00024         void f() {
00025                 std::cout << "A::f()" << std::endl;
00026         }
00027         void g(const int &i) {
00028                 std::cout << "A::g(" << i << ")" << std::endl;
00029         }
00030 };
00031 
00032 class NullaryFunction {
00033 public:
00034         NullaryFunction() : i(0) {};
00035         typedef void result_type;
00036         void operator()() {
00037                 for (int j = 0; j < 3; ++j ) {
00038                         std::cout << "  Nullary Function Object: " << i << std::endl;
00039                         ecl::Sleep sleep;
00040                         sleep(1);
00041                         ++i;
00042                 }
00043                 std::cout << "  Nullary Function Object: finished." << std::endl;
00044         }
00045         int i;
00046 };
00047 
00048 void d() {
00049         for (int i = 0; i < 5; ++i ) {
00050                 ecl::Sleep sleep;
00051                 sleep(1);
00052                 std::cout << "  d(): " << i << std::endl;
00053         }
00054         std::cout << "  d(): finished" << std::endl;
00055 }
00056 void d_cb() {
00057         std::cout << "  d(): callback function" << std::endl;
00058 }
00059 
00060 void f() {
00061         for (int i = 0; i < 3; ++i) {
00062                 std::cout << "f(): " << i << std::endl;
00063         }
00064 }
00065 
00066 void g(const int &i) {
00067         for (int j = 0; j < 3; ++j) {
00068                 std::cout << "g(" << i << ")" << std::endl;
00069         }
00070 }
00071 
00072 void deconstructThread() {
00073         ecl::Thread thread_deconstruct(d);
00074 }
00075 
00076 int main() {
00077 
00078     std::cout << std::endl;
00079     std::cout << "***********************************************************" << std::endl;
00080     std::cout << "                   Thread Function Type" << std::endl;
00081     std::cout << "***********************************************************" << std::endl;
00082     std::cout << std::endl;
00083 
00084     ThreadMemberFunctions a;
00085     ecl::Thread thread_f1(f);
00086     ecl::Thread thread_f2(&ThreadMemberFunctions::f, a);
00087     ecl::Thread thread_f3(ecl::generateFunctionObject(f));
00088     ecl::Thread thread_f4(ecl::generateFunctionObject(g,1));
00089     ecl::Thread thread_f5(ecl::generateFunctionObject(&ThreadMemberFunctions::f,a));
00090     ecl::Thread thread_f6(ecl::generateFunctionObject(&ThreadMemberFunctions::g,a,2));
00091 
00092     thread_f1.join();
00093     thread_f2.join();
00094     thread_f3.join();
00095     thread_f4.join();
00096     thread_f5.join();
00097     thread_f6.join();
00098 
00099     std::cout << std::endl;
00100     std::cout << "***********************************************************" << std::endl;
00101     std::cout << "               Cancel" << std::endl;
00102     std::cout << "***********************************************************" << std::endl;
00103     std::cout << std::endl;
00104 
00105         ecl::Thread thread(ecl::generateFunctionObject(d));
00106         ecl::Sleep sleep;
00107         sleep(3);
00108         if (!thread.isRunning()) {
00109                 std::cout << "Abnormal #1" << std::endl;
00110         }
00111         thread.cancel();
00112         if (thread.isRunning()) {
00113                 std::cout << "Abnormal #2" << std::endl;
00114         }
00115     thread.join();
00116         if (thread.isRunning()) {
00117                 std::cout << "Abnormal #3" << std::endl;
00118         }
00119 
00120     std::cout << std::endl;
00121     std::cout << "***********************************************************" << std::endl;
00122     std::cout << "               Deconstruct" << std::endl;
00123     std::cout << "***********************************************************" << std::endl;
00124     std::cout << std::endl;
00125 
00126     deconstructThread();
00127     sleep(6);
00128 
00129     std::cout << std::endl;
00130     std::cout << "***********************************************************" << std::endl;
00131     std::cout << "               Nullary Function Objects" << std::endl;
00132     std::cout << "***********************************************************" << std::endl;
00133     std::cout << std::endl;
00134 
00135     NullaryFunction function_object_1;
00136     ecl::Thread thread_f8(function_object_1);
00137     sleep(4);
00138     ecl::Thread thread_f9(ecl::ref(function_object_1));
00139         sleep(4);
00140     thread_f8.join();
00141     thread_f9.join();
00142 
00143     std::cout << std::endl;
00144     std::cout << "***********************************************************" << std::endl;
00145     std::cout << "               Delayed Start" << std::endl;
00146     std::cout << "***********************************************************" << std::endl;
00147     std::cout << std::endl;
00148 
00149         NullaryFunction function_object_2;
00150         ThreadMemberFunctions b;
00151         ecl::Thread thread1, thread2, thread3, thread4;
00152         thread1.start(f);
00153         thread2.start(function_object_2);
00154         thread3.start(&ThreadMemberFunctions::f,b);
00155         thread4.start(ecl::generateFunctionObject(&ThreadMemberFunctions::g,b,2));
00156 
00157         thread1.join();
00158         thread2.join();
00159         thread3.join();
00160         thread4.join();
00161 
00162     std::cout << std::endl;
00163     std::cout << "***********************************************************" << std::endl;
00164     std::cout << "                      Passed" << std::endl;
00165     std::cout << "***********************************************************" << std::endl;
00166     std::cout << std::endl;
00167 
00168     return 0;
00169 }


ecl_threads
Author(s): Daniel Stonier
autogenerated on Sun Oct 5 2014 23:35:37