source: Sophya/trunk/SophyaLib/SysTools/zthread.cc@ 2598

Last change on this file since 2598 was 2598, checked in by ansari, 21 years ago

Documentation (ajoutee ou completee) pour les classes du module SysTools - Reza 11 Aout 2004

File size: 4.2 KB
Line 
1// Interface C++ aux Thread POSIX - R. Ansari 02/2001
2// LAL (Orsay) / IN2P3-CNRS DAPNIA/SPP (Saclay) / CEA
3
4#include "zthread.h"
5#include <stdlib.h>
6#include <stdio.h>
7
8/*!
9 \class SOPHYA::ZThread
10 \ingroup SysTools
11 \brief Simple class for creating and controlling threads.
12
13 This class provides an interface for creating and controlling threads.
14 The implementation uses the POSIX thread interface.
15 The ZThread objects can be sub classed with the redefinition of
16 the \c run() method which then performs the task.
17 The default \c run() method of the base class can be used directly
18 to perform computation through a function (see \b setAction() method)
19 \sa SOPHYA::ZMutex
20 The following sample code shows the usage of ZThread object
21 to run simultaneously two functions to perform computation.
22
23 \code
24 // The functions to perform computing
25 void fun1(void *arg) { }
26 void fun2(void *arg) { }
27 // ...
28 ZThread zt1;
29 zt1.setAction(fun1, arg[1]);
30 ZThread zt2;
31 zt2.setAction(fun2, arg[1]);
32 cout << " Starting threads ... " << endl;
33 zt1.start();
34 zt2.start();
35 cout << " Waiting for threads to end ... " << endl;
36 zt1.join();
37 zt2.join();
38 \endcode
39*/
40
41#define CheckSt(st_, strg_) if (st_ != 0) perror(strg_);
42
43/*
44extern "C" {
45 void * zthr_run( void * xthr);
46}
47*/
48
49static void * zthr_run(void * xthr)
50{
51 ZThread * thr = (ZThread *)xthr;
52 thr->run();
53 pthread_exit(NULL);
54 return NULL;
55}
56
57
58/* ------ Classe ZThread ------- */
59/*!
60 Constructor, with optional specification of the thread stack size.
61*/
62ZThread::ZThread(size_t stacksize)
63{
64 _initok = false;
65 _ssize = 0;
66 _act = NULL;
67 _usp = NULL;
68 _rc = -99;
69}
70
71ZThread::~ZThread()
72{
73 // Que faut-il faire ?
74}
75
76/*!
77 Method which starts the thread.
78*/
79void ZThread::start()
80{
81 if (_initok) throw ZThreadExc("ZThread::Start() - Already started thread !");
82 int rc;
83 pthread_attr_t tha;
84 rc = pthread_attr_init(&tha);
85 CheckSt(rc,"ZThread::start() - Pb creating tha attribute object");
86 if (_ssize > 0)
87 rc = pthread_attr_setstacksize(&tha, _ssize);
88 rc = pthread_create(&_thr, &tha, zthr_run, this);
89 CheckSt(rc,"ZThread::start() - Pb creating the thread object");
90 _initok = true;
91 setRC(rc);
92}
93
94/*!
95 Calls the pthread_cancel. Can be used to stop a running thread.
96*/
97void ZThread::cancel()
98{
99 if (!_initok) throw ZThreadExc("ZThread::cancel() - thread not started !");
100 int rc = pthread_cancel(_thr);
101 CheckSt(rc,"ZThread::cancel() - Pb pthread_cancel() ");
102 setRC(-77);
103}
104
105
106/*!
107 Waits for the thread to terminate.
108*/
109void ZThread::join()
110{
111 if (!_initok) throw ZThreadExc("ZThread::join() - thread not started !");
112 void * x;
113 int rc = pthread_join(_thr, &x);
114 CheckSt(rc,"ZThread::Join() - Pb pthread_join() ");
115 return;
116}
117
118/*!
119 This virtual method can be redefined in the derived class, in order
120 to perform the actual computation.
121*/
122void ZThread::run()
123{
124 if (_act) _act(_usp);
125 setRC(0);
126 return;
127}
128
129
130/* ------ Classe ZMutex ------- */
131/*!
132 \class SOPHYA::ZMutex
133 \ingroup SysTools
134 \brief Wrapper for Mutex objects.
135
136 This class implements an interface to the Mutual Exclusion objects
137 of the POSIX threads.
138 The ZMutex objects should be used to control acces from different threads
139 to common objects through the \b lock() and \b unlock() methods.
140 \sa SOPHYA::ZSync
141*/
142
143/*!
144 Constructor: Creates a associated pair of POSIX \c pthread_mutex_t
145 and \c pthread_cond_t objects
146*/
147ZMutex::ZMutex()
148{
149 _mutx = new pthread_mutex_t;
150 _condv = new pthread_cond_t;
151 int rc;
152 rc = pthread_mutex_init(_mutx, NULL);
153 CheckSt(rc,"ZMutex::ZMutex() Pb pthread_mutex_init");
154 rc = pthread_cond_init(_condv, NULL);
155 CheckSt(rc,"ZMutex::ZMutex() Pb pthread_cond_init");
156}
157
158ZMutex::~ZMutex()
159{
160 delete _mutx;
161 delete _condv;
162}
163
164/*!
165 \class SOPHYA::ZSync
166 \ingroup SysTools
167 \brief Wrapper/utility class ensuring synchronised execution of an instruction bloc.
168
169 This class can be used to insure that the execution of a given
170 part of the code is synchronised, i.e. only a single thread
171 goes through this at a given time.
172 The constructor acquires a lock on given \b ZMutex object,
173 which is released by the destructor.
174 The ZSync object should then be instanciated at the beginning of
175 the synchronised instruction bloc.
176 \sa SOPHYA::ZMutex
177*/
Note: See TracBrowser for help on using the repository browser.