threads.cpp
Go to the documentation of this file.
00001 
00008 /*****************************************************************************
00009 ** Platform Check
00010 *****************************************************************************/
00011 
00012 #include <iostream>
00013 #include <ecl/config/ecl.hpp>
00014 #if defined(ECL_IS_POSIX)
00015 
00016 /*****************************************************************************
00017 ** Includes
00018 *****************************************************************************/
00019 
00020 #include <gtest/gtest.h>
00021 #include <ecl/config/ecl.hpp>
00022 #include <ecl/exceptions/standard_exception.hpp>
00023 #include <ecl/utilities/function_objects.hpp>
00024 #include <ecl/utilities/references.hpp>
00025 #include "../../include/ecl/threads/thread.hpp"
00026 
00027 /*****************************************************************************
00028 ** Doxygen
00029 *****************************************************************************/
00034 /*****************************************************************************
00035 ** Using
00036 *****************************************************************************/
00037 
00038 using ecl::StandardException;
00039 using ecl::Thread;
00040 using ecl::generateFunctionObject;
00041 using ecl::ref;
00042 
00043 /*****************************************************************************
00044 ** Globals
00045 *****************************************************************************/
00046 
00047 // Change this if you want std output.
00048 static const bool output = false;
00049 
00050 
00051 /*****************************************************************************
00052 ** Namespaces
00053 *****************************************************************************/
00054 
00055 namespace ecl {
00056 namespace threads {
00057 namespace tests {
00058 
00059 /*****************************************************************************
00060 ** Classes
00061 *****************************************************************************/
00062 
00063 class ThreadMemberFunctions {
00064 public:
00065         void f() {
00066                 if ( output ) {
00067                         std::cout << "A::f()" << std::endl;
00068                 }
00069         }
00070         void g(const int &i) {
00071                 if ( output ) {
00072                         std::cout << "A::g(" << i << ")" << std::endl;
00073                 }
00074         }
00075 };
00076 
00077 class NullaryFunction {
00078 public:
00079         NullaryFunction() : i(0) {};
00080         typedef void result_type;
00081         void operator()() {
00082                 for (int j = 0; j < 3; ++j ) {
00083                         if ( output ) {
00084                                 std::cout << "  Nullary Function Object: " << i << std::endl;
00085                         }
00086                         sleep(1);
00087                         ++i;
00088                 }
00089                 if ( output ) {
00090                         std::cout << "  Nullary Function Object: finished." << std::endl;
00091                 }
00092         }
00093         int i;
00094 };
00095 
00096 /*****************************************************************************
00097 ** Functions
00098 *****************************************************************************/
00099 
00100 void d() {
00101         for (int i = 0; i < 5; ++i ) {
00102                 sleep(1);
00103                 if ( output ) {
00104                         std::cout << "  d(): " << i << std::endl;
00105                 }
00106         }
00107         if ( output ) {
00108                 std::cout << "  d(): finished" << std::endl;
00109         }
00110 }
00111 void d_cb() {
00112         if ( output ) {
00113                 std::cout << "  d(): callback function" << std::endl;
00114         }
00115 }
00116 
00117 void f() {
00118         for (int i = 0; i < 3; ++i) {
00119                 if ( output ) {
00120                         std::cout << "f(): " << i << std::endl;
00121                 }
00122         }
00123 }
00124 
00125 void g(const int &i) {
00126         for (int j = 0; j < 3; ++j) {
00127                 if ( output ) {
00128                         std::cout << "g(" << i << ")" << std::endl;
00129                 }
00130         }
00131 }
00132 
00133 void deconstructThread() {
00134         Thread thread_deconstruct(d);
00135 }
00136 
00137 } // namespace Tests
00138 } // namespace threads
00139 } // namespace ecl
00140 
00141 /*****************************************************************************
00142 ** Using
00143 *****************************************************************************/
00144 
00145 using namespace ecl::threads::tests;
00146 
00147 /*****************************************************************************
00148 ** Doxygen
00149 *****************************************************************************/
00150 
00155 /*****************************************************************************
00156 ** Tests
00157 *****************************************************************************/
00158 
00159 TEST(ThreadTests,threadFunctionTypes) {
00160     ThreadMemberFunctions a;
00161     Thread thread_f1(f);
00162     Thread thread_f2(&ThreadMemberFunctions::f,a);
00163     Thread thread_f3(generateFunctionObject(f));
00164     Thread thread_f4(generateFunctionObject(g,1));
00165     Thread thread_f5(generateFunctionObject(&ThreadMemberFunctions::f,a));
00166     Thread thread_f6(generateFunctionObject(&ThreadMemberFunctions::g,a,2));
00167     thread_f1.join();
00168     thread_f2.join();
00169     thread_f3.join();
00170     thread_f4.join();
00171     thread_f5.join();
00172     thread_f6.join();
00173     SUCCEED();
00174 }
00175 
00176 TEST(ThreadTests,cancelThread) {
00177         Thread thread(generateFunctionObject(d));
00178         sleep(3);
00179         EXPECT_TRUE(thread.isRunning());
00180 //      std::cout << "Cancelling d() thread (you should see no further output)." << std::endl;
00181         thread.cancel();
00182         EXPECT_FALSE(thread.isRunning());
00183     thread.join();
00184         EXPECT_FALSE(thread.isRunning());
00185 }
00186 
00187 TEST(ThreadTests,deconstruct) {
00188     deconstructThread();
00189     sleep(6);
00190     SUCCEED();
00191 }
00192 
00193 TEST(ThreadTests,nullaryFunctionObjects) {
00194     NullaryFunction function_object;
00195     Thread thread_f8(function_object); //    Thread thread_f8 = NullaryFunction();
00196     sleep(4);
00197     Thread thread_f9(ref(function_object));
00198         sleep(4);
00199     thread_f8.join();
00200     thread_f9.join();
00201         SUCCEED();
00202 }
00203 
00204 TEST(ThreadTests,stackSize) {
00205     Thread thread(f,ecl::DefaultPriority,1024*1024);
00206     thread.join();
00207     SUCCEED();
00208 }
00209 
00210 TEST(ThreadTests,delayedStart) {
00211 
00212         NullaryFunction function_object;
00213         ThreadMemberFunctions a;
00214         Thread thread1, thread2, thread3, thread4;
00215         thread1.start(f);
00216         thread2.start(function_object);
00217         thread3.start(&ThreadMemberFunctions::f,a);
00218         thread4.start(generateFunctionObject(&ThreadMemberFunctions::g,a,2));
00219         thread1.join();
00220         thread2.join();
00221         thread3.join();
00222         thread4.join();
00223     SUCCEED();
00224 }
00225 
00226 /*****************************************************************************
00227 ** Main program
00228 *****************************************************************************/
00229 
00230 int main(int argc, char **argv) {
00231 
00232     testing::InitGoogleTest(&argc,argv);
00233     return RUN_ALL_TESTS();
00234 }
00235 
00236 #else
00237 
00238 /*****************************************************************************
00239 ** Alternative main
00240 *****************************************************************************/
00241 
00242 int main(int argc, char **argv) {
00243 
00244         std::cout << "Currently not supported on your platform." << std::endl;
00245 }
00246 
00247 #endif /* ECL_IS_POSIX */
00248 


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