Changeset 2212 in Sophya for trunk/SophyaLib/SysTools/zthread.cc
- Timestamp:
- Oct 15, 2002, 5:48:53 PM (23 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/SophyaLib/SysTools/zthread.cc
r1612 r2212 5 5 #include <stdlib.h> 6 6 #include <stdio.h> 7 8 /*! 9 \class SOPHYA::ZThread 10 \ingroup SysTools 11 This class provides an interface for creating and controlling threads. 12 The implementation uses the POSIX thread interface. 13 The ZThread objects can be sub classed with the redefinition of 14 the \c run() method which then performs the task. 15 The default \c run() method of the base class can be used directly 16 to perform computation through a function (see \b setAction() method) 17 \sa SOPHYA::ZMutex 18 The following sample code shows the usage of ZThread object 19 to run simultaneously to functions to perform computation. 20 21 \code 22 // The functions to perform computing 23 void fun1(void *arg) { } 24 void fun2(void *arg) { } 25 // ... 26 ZThread zt1; 27 zt1.setAction(fun1, arg[1]); 28 ZThread zt2; 29 zt2.setAction(fun2, arg[1]); 30 cout << " Starting threads ... " << endl; 31 zt1.start(); 32 zt2.start(); 33 cout << " Waiting for threads to end ... " << endl; 34 zt1.join(); 35 zt2.join(); 36 \endcode 37 */ 7 38 8 39 #define CheckSt(st_, strg_) if (st_ != 0) perror(strg_); … … 24 55 25 56 /* ------ Classe ZThread ------- */ 26 57 /*! 58 Constructor, with optional specification of the thread stack size. 59 */ 27 60 ZThread::ZThread(size_t stacksize) 28 61 { … … 39 72 } 40 73 74 /*! 75 Method which starts the thread. 76 */ 41 77 void ZThread::start() 42 78 { … … 54 90 } 55 91 56 92 /*! 93 Calls the pthread_cancel. Can be used to stop a running thread. 94 */ 57 95 void ZThread::cancel() 58 96 { … … 64 102 65 103 104 /*! 105 Waits for the thread to terminate. 106 */ 66 107 void ZThread::join() 67 108 { … … 73 114 } 74 115 116 /*! 117 This virtual method can be redefined in the derived class, in order 118 to perform the actual computation. 119 */ 75 120 void ZThread::run() 76 121 { … … 82 127 83 128 /* ------ Classe ZMutex ------- */ 129 /*! 130 \class SOPHYA::ZMutex 131 \ingroup SysTools 132 This class implements an interface to the Mutual Exclusion objects 133 of the POSIX threads. 134 The ZMutex objects should be used to control acces from different threads 135 to common objects through the \b lock() and \b unlock() methods. 136 \sa SOPHYA::ZSync 137 */ 84 138 139 /*! 140 Constructor: Creates a associated pair of POSIX \c pthread_mutex_t 141 and \c pthread_cond_t objects 142 */ 85 143 ZMutex::ZMutex() 86 144 { … … 100 158 } 101 159 160 /*! 161 \class SOPHYA::ZSync 162 \ingroup SysTools 163 This class can be used to insure that the execution of a given 164 part of the code is synchronised, i.e. only a single thread 165 goes through this at a given time. 166 The constructor acquires a lock on given \b ZMutex object, 167 which is released by the destructor. 168 The ZSync object should then be instanciated at the beginning of 169 the synchronised instruction bloc. 170 \sa SOPHYA::ZMutex 171 */
Note:
See TracChangeset
for help on using the changeset viewer.