source: trunk/MagicSoft/Cosy/base/MThread.cc@ 1683

Last change on this file since 1683 was 1275, checked in by tbretz, 23 years ago
*** empty log message ***
File size: 3.5 KB
Line 
1#include <MThread.h>
2
3#include <iostream.h>
4
5#include <pthread.h>
6#include <sys/resource.h> // PRIO_PROCESS
7
8// ----------------------------------------------------------------------
9//
10// Constructor
11//
12// Starts the derived thread if you don't specify false.
13//
14MThread::MThread(bool start, int prio) : fIsRunning(false), fIsDetached(false), fThread(NULL), fReturn(NULL)
15{
16 if (!start)
17 return;
18
19 SetPriority(prio);
20 Start();
21}
22
23// ----------------------------------------------------------------------
24//
25// Destructor
26//
27// Stops the derived thread if it is still running.
28//
29MThread::~MThread()
30{
31 cout << "~MThread::MThread" << endl;
32 Stop();
33}
34
35// ----------------------------------------------------------------------
36//
37// Detach the derived thread. This means, that if the thread ends the
38// needed resources (eg. for storing the return code) are freed.
39// In other word you cannot get any value back from the Thread.
40//
41void MThread::Detach()
42{
43 if (fIsRunning)
44 pthread_detach(*fThread);
45
46 fIsDetached = true;
47}
48
49// ----------------------------------------------------------------------
50//
51// Sets the priority of the thread.
52// -20 highest priority
53// 0 standard
54// +20 lowest priority
55// This can only be done before the thread is started.
56//
57bool MThread::SetPriority(int prio)
58{
59 if (fIsRunning)
60 return false;
61
62 fPriority = prio;
63 return true;
64}
65
66// ----------------------------------------------------------------------
67//
68// Now we are back in a class instance, but running in new thread.
69// All class members can be accessed like before.
70// Set the flag for a running thread. Check if the thread should get
71// detached. Set the priority of the thread. Now reset the stop flag and
72// execute the thread. After the thread stopped it's execution reset the
73// running flag and exit.
74//
75void *MThread::RunThread()
76{
77 fIsRunning = true;
78
79 if (fIsDetached)
80 pthread_detach(pthread_self());
81
82 setpriority(PRIO_PROCESS, 0, fPriority); //lowest priority
83
84 fStop = false;
85
86 void *rc = Thread();
87
88 fIsRunning = false;
89
90 return rc;
91}
92
93// ----------------------------------------------------------------------
94//
95// Get the Instance back from the thread argument and call the
96// RunThread member function, which handles all MThread bspecific stuff.
97//
98void *MThread::MapThread(void *arg)
99{
100 MThread *thread = (MThread*)arg;
101
102 return thread->RunThread();
103}
104
105// ----------------------------------------------------------------------
106//
107// A thread is created and started (MapThread).
108// As an argument the actual instance is used.
109//
110void MThread::Start()
111{
112 fThread = new pthread_t;
113 pthread_create(fThread, NULL, MapThread, this);
114}
115
116// ----------------------------------------------------------------------
117//
118// Check if a thread is existing and running.
119// If the thread is detached, cancel the thread. Otherwise set the stop
120// flag and wait for the thread to exit.
121//
122void MThread::Stop()
123{
124 cout << "MThread::Stop: fThread=" << fThread << ", fIsRunning=" << (int)fIsRunning << endl;
125 if (!fThread || !fIsRunning)
126 return;
127
128 if (fIsDetached)
129 {
130 cout << "Stopping detached thread..." << flush;
131 pthread_cancel(*fThread);
132 fIsRunning = false;
133 }
134 else
135 {
136 cout << "Stopping thread..." << flush;
137 fStop = true;
138 pthread_join(*fThread, &fReturn);
139 }
140 cout << "done." << endl;
141
142 delete fThread;
143 fThread = NULL;
144
145 cout << "MThread::Stop() done." << endl;
146}
Note: See TracBrowser for help on using the repository browser.