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

Last change on this file since 2308 was 2212, checked in by ansari, 23 years ago

Mise a jour de la documentation SysTools, Reza 15/10/2002

File size: 4.1 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 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*/
38
39#define CheckSt(st_, strg_) if (st_ != 0) perror(strg_);
40
41/*
42extern "C" {
43 void * zthr_run( void * xthr);
44}
45*/
46
47static void * zthr_run(void * xthr)
48{
49 ZThread * thr = (ZThread *)xthr;
50 thr->run();
51 pthread_exit(NULL);
52 return NULL;
53}
54
55
56/* ------ Classe ZThread ------- */
57/*!
58 Constructor, with optional specification of the thread stack size.
59*/
60ZThread::ZThread(size_t stacksize)
61{
62 _initok = false;
63 _ssize = 0;
64 _act = NULL;
65 _usp = NULL;
66 _rc = -99;
67}
68
69ZThread::~ZThread()
70{
71 // Que faut-il faire ?
72}
73
74/*!
75 Method which starts the thread.
76*/
77void ZThread::start()
78{
79 if (_initok) throw ZThreadExc("ZThread::Start() - Already started thread !");
80 int rc;
81 pthread_attr_t tha;
82 rc = pthread_attr_init(&tha);
83 CheckSt(rc,"ZThread::start() - Pb creating tha attribute object");
84 if (_ssize > 0)
85 rc = pthread_attr_setstacksize(&tha, _ssize);
86 rc = pthread_create(&_thr, &tha, zthr_run, this);
87 CheckSt(rc,"ZThread::start() - Pb creating the thread object");
88 _initok = true;
89 setRC(rc);
90}
91
92/*!
93 Calls the pthread_cancel. Can be used to stop a running thread.
94*/
95void ZThread::cancel()
96{
97 if (!_initok) throw ZThreadExc("ZThread::cancel() - thread not started !");
98 int rc = pthread_cancel(_thr);
99 CheckSt(rc,"ZThread::cancel() - Pb pthread_cancel() ");
100 setRC(-77);
101}
102
103
104/*!
105 Waits for the thread to terminate.
106*/
107void ZThread::join()
108{
109 if (!_initok) throw ZThreadExc("ZThread::join() - thread not started !");
110 void * x;
111 int rc = pthread_join(_thr, &x);
112 CheckSt(rc,"ZThread::Join() - Pb pthread_join() ");
113 return;
114}
115
116/*!
117 This virtual method can be redefined in the derived class, in order
118 to perform the actual computation.
119*/
120void ZThread::run()
121{
122 if (_act) _act(_usp);
123 setRC(0);
124 return;
125}
126
127
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*/
138
139/*!
140 Constructor: Creates a associated pair of POSIX \c pthread_mutex_t
141 and \c pthread_cond_t objects
142*/
143ZMutex::ZMutex()
144{
145 _mutx = new pthread_mutex_t;
146 _condv = new pthread_cond_t;
147 int rc;
148 rc = pthread_mutex_init(_mutx, NULL);
149 CheckSt(rc,"ZMutex::ZMutex() Pb pthread_mutex_init");
150 rc = pthread_cond_init(_condv, NULL);
151 CheckSt(rc,"ZMutex::ZMutex() Pb pthread_cond_init");
152}
153
154ZMutex::~ZMutex()
155{
156 delete _mutx;
157 delete _condv;
158}
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 TracBrowser for help on using the repository browser.