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

Last change on this file since 2887 was 2759, checked in by ansari, 20 years ago

Amelioration de l'interface ZSync() + ajout commentaires - Reza 24/05/2005

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