![]() |
Main Page
Class Hierarchy
Alphabetical List
Compound List
File List
Compound Members
![]() |
00001 /******************************************************************************** 00002 * * 00003 * Thread object for worker functions * 00004 * * 00005 ********************************************************************************* 00006 * Copyright (C) 2003 by Mathew Robertson. All Rights Reserved. * 00007 ********************************************************************************* 00008 * This library is free software; you can redistribute it and/or * 00009 * modify it under the terms of the GNU Lesser General Public * 00010 * License as published by the Free Software Foundation; either * 00011 * version 2.1 of the License, or (at your option) any later version. * 00012 * * 00013 * This library is distributed in the hope that it will be useful, * 00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * 00016 * Lesser General Public License for more details. * 00017 * * 00018 * You should have received a copy of the GNU Lesser General Public * 00019 * License along with this library; if not, write to the Free Software * 00020 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. * 00021 *********************************************************************************/ 00022 #ifndef FXTHREADFUNCTION_H 00023 #define FXTHREADFUNCTION_H 00024 00025 #ifndef FXRUNNABLE_H 00026 #include "FXRunnable.h" 00027 #endif 00028 namespace FXEX { 00029 00030 /** 00031 * You can start a worker thread for running your task. It runs with a lower priority 00032 * than your main thread. It runs as a detached thread (ie you main thread cant/shouldn't 00033 * wait for it to finish). example: 00034 * 00035 * // declare file scope pointer to variable 00036 * FXThreadFunction *thread; 00037 * // declare some variables... 00038 * 00039 * // do some work 00040 * void workerFunction(){ 00041 * while (...) { 00042 * // use global variables, including some atomic's 00043 * ... 00044 * if (..) thread->signal(); 00045 * ... 00046 * } 00047 * } 00048 * 00049 * // FOX message map - handle a thread event 00050 * ... 00051 * FXMAPFUNC(SEL_CREATE,MyApp::ID_WORKER,MyApp:onWorker), 00052 * FXMAPFUNC(SEL_THREAD,MyApp::ID_WORKER,MyApp:onWorker), 00053 * FXMAPFUNC(SEL_DESTROY,MyApp::ID_WORKER,MyApp:onWorker), 00054 * ... 00055 * 00056 * // main window/thread 00057 * MyApp::MyApp(a):FXMainWindow(a){ 00058 * ... 00059 * // setup variables 00060 * ... 00061 * // create the thread 00062 * thread = new FXThreadFunction(workerFunction,this,ID_WORKER); 00063 * ... 00064 * } 00065 * 00066 * MyApp::create(){ 00067 * thread->start(); 00068 * } 00069 * 00070 * // handle thread event 00071 * long MyApp::onWorker(,,){ 00072 * // update something 00073 * // reset variables 00074 * return 1; 00075 * } 00076 * 00077 * 00078 * 00079 * alternatively: 00080 * 00081 * 00082 * 00083 * FXAtomicInt atomicInt; 00084 * 00085 * MyApp::MyApp(a):FXMainWindow(a){ 00086 * ... 00087 * atomicInt=0; 00088 * atomicIntDataTarget.connect(atomicInt); 00089 * thread = new FXThreadFunction(workerFunction,...); 00090 * ... 00091 * new FXTextField(this,10,atomicIntDataTarget,FXDataTarget::ID_VALUE,...); 00092 * ... 00093 * } 00094 * 00095 * void MyApp::workerFunction(){ 00096 * while (...) { 00097 * // do some work 00098 * ... 00099 * atomicInt++; 00100 * } 00101 * } 00102 * 00103 */ 00104 class FXAPI FXThreadFunction : public FXRunnable { 00105 FXDECLARE(FXThreadFunction) 00106 00107 private: 00108 FXThreadFunction(const FXThreadFunction&); 00109 FXThreadFunction& operator=(const FXThreadFunction&); 00110 00111 private: 00112 FXThreadHandle thread; // the thread 00113 00114 protected: 00115 FXThreadFunction() {} 00116 00117 public: 00118 /// Note: you dont normally need to call these, they are used internally but must be public 00119 00120 /// reset the state of the thread object, so that it can be reused 00121 void reset(); 00122 00123 /// handle to thread function 00124 void (*threadFunction)(); 00125 00126 public: 00127 /// Pass a reference to your static method to this ctor 00128 FXThreadFunction(void (*func)(),FXObject *tgt=NULL,FXSelector sel=0); 00129 00130 /// set to new function 00131 void setFunc(void (*func)()); 00132 00133 /// start running the object 00134 void start(); 00135 00136 /// dtor 00137 virtual ~FXThreadFunction(); 00138 }; 00139 00140 } // namespace FXEX 00141 #endif // FXTHREADFUNCTION_H