-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathThread.h
87 lines (60 loc) · 1.57 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// Copyright (c) 2013 Hyperceptive, LLC
// 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,
Suspended,
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);
void suspend();
void resume();
// Signal a thread to stop.
void stop();
inline threadStatus getStatus() const { return _status; }
bool isDone() const;
protected:
void checkSuspend(); //call by derived class if suspend is needed.
// 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;
pthread_mutex_t _suspendMutex;
pthread_cond_t _resumeCondition;
};
inline bool Thread::isDone() const
{
if (_status == Created || _status == Running || _status == Suspended)
{
return false;
}
else // Finished || Invalid
{
return true;
}
}
#endif