Fawkes API Fawkes Development Version
|
00001 00002 /*************************************************************************** 00003 * example_waitcond.cpp - wait condition example program 00004 * 00005 * Created: Sat Mar 01 15:13:44 2008 00006 * Copyright 2006-2008 Tim Niemueller [www.niemueller.de] 00007 * 00008 ****************************************************************************/ 00009 00010 /* This program is free software; you can redistribute it and/or modify 00011 * it under the terms of the GNU General Public License as published by 00012 * the Free Software Foundation; either version 2 of the License, or 00013 * (at your option) any later version. 00014 * 00015 * This program is distributed in the hope that it will be useful, 00016 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00017 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00018 * GNU Library General Public License for more details. 00019 * 00020 * Read the full text in the LICENSE.GPL file in the doc directory. 00021 */ 00022 00023 // Do not mention in API doc 00024 /// @cond EXAMPLES 00025 00026 #include <core/threading/thread.h> 00027 #include <core/threading/wait_condition.h> 00028 #include <core/exception.h> 00029 #include <core/threading/mutex.h> 00030 00031 #include <iostream> 00032 #include <string> 00033 00034 using namespace std; 00035 using namespace fawkes; 00036 00037 typedef enum { 00038 WAITER, 00039 WAKER 00040 } threadmode_t; 00041 00042 class ExampleWaitCondThread : public Thread 00043 { 00044 public: 00045 ExampleWaitCondThread(threadmode_t mode, string tname, 00046 WaitCondition *waitcond, unsigned int sleep_time) 00047 : Thread(tname.c_str(), Thread::OPMODE_CONTINUOUS) 00048 { 00049 __mode = mode; 00050 __waitcond = waitcond; 00051 __sleep_time = sleep_time; 00052 } 00053 00054 virtual void loop() 00055 { 00056 if ( __mode == WAITER ) { 00057 usleep( __sleep_time ); 00058 cout << name() << ": Waiting for waker" << endl; 00059 try { 00060 __waitcond->wait(); 00061 cout << name() << ": Woken up" << endl; 00062 } catch (Exception &e) { 00063 cout << name() << ": EXCEPTION" << endl; 00064 e.print_trace(); 00065 } 00066 } else { // WAKER 00067 usleep( __sleep_time ); 00068 cout << name() << ": Waking waiter" << endl; 00069 __waitcond->wake_all(); 00070 cout << name() << ": Woke waiter" << endl; 00071 } 00072 } 00073 00074 private: 00075 threadmode_t __mode; 00076 WaitCondition *__waitcond; 00077 unsigned int __sleep_time; 00078 00079 }; 00080 00081 00082 int 00083 main(int argc, char **argv) 00084 { 00085 WaitCondition *w = new WaitCondition(); 00086 00087 ExampleWaitCondThread *t1 = new ExampleWaitCondThread(WAITER, "waiter1", w, 0); 00088 ExampleWaitCondThread *t2 = new ExampleWaitCondThread(WAITER, "waiter2", w, 0); 00089 ExampleWaitCondThread *tw = new ExampleWaitCondThread(WAKER, "waker", w, 2458642); 00090 00091 t1->start(); 00092 t2->start(); 00093 tw->start(); 00094 00095 t1->join(); 00096 t2->join(); 00097 tw->join(); 00098 00099 delete t1; 00100 delete t2; 00101 delete tw; 00102 00103 delete w; 00104 } 00105 00106 00107 /// @endcond