-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathThread.h
72 lines (49 loc) · 1.32 KB
/
Thread.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// Copyright (c) 2013 Matt Hill
// Use of this source code is governed by The MIT License
// that can be found in the LICENSE file.
//
// Very basic abstract class for creating a POSIX thread on a Raspberry Pi.
//
// Class was developed using Wheezy (Linux).
#ifndef RPI_THREAD_H
#define RPI_THREAD_H
#include <pthread.h>
class Thread
{
public:
enum threadStatus { Created, Running, Finished, Invalid };
Thread();
virtual ~Thread();
// Start the thread and execute the run() method.
// Priority >0 will run thread as realtime.
void start(int priority = 0);
// Signal a thread to stop.
void stop();
inline threadStatus getStatus() const { return _status; }
bool isDone() const;
protected:
// Check if thread was signaled to stop.
inline bool shouldStop() { return _stopThread; }
// Force a thread down.
void terminate(unsigned long i_return);
private:
static void *executeThread(void *tobject);
// Thread worker method. Override in derived class.
virtual void run();
pthread_t _thread;
volatile threadStatus _status;
bool _stopThread;
bool _threadDown;
};
inline bool Thread::isDone() const
{
if (_status == Created || _status == Running)
{
return false;
}
else // Finished || Invalid
{
return true;
}
}
#endif