forked from augcampos/asterisk-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThread.cpp
More file actions
73 lines (57 loc) · 1.56 KB
/
Thread.cpp
File metadata and controls
73 lines (57 loc) · 1.56 KB
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
/*
* Thread.cpp
*
* Created on: Aug 23, 2011
* Author: augcampos
*/
#include "asteriskcpp/structs/Thread.h"
#include <iostream>
#include "asteriskcpp/utils/LogHandler.h"
namespace asteriskcpp {
Thread::Thread() {
m_thread = NULL;
}
Thread::~Thread() {
stop();
if (m_thread != NULL) {
delete (m_thread);
}
}
void Thread::start() {
setMustStop(false);
if (m_thread != NULL) {
delete (m_thread);
}
// Create thread and start it with myself as argument. Pass myself as reference since I don't want a copy
m_thread = new boost::thread(boost::ref(*this));
}
void Thread::stop() {
// Signal the thread to stop (thread-safe)
setMustStop(true);
// Wait for the thread to finish.
if (m_thread != NULL) {
if (m_thread->get_id() != boost::this_thread::get_id()) {
m_thread->interrupt();
m_thread->join();
delete (m_thread);
m_thread = NULL;
}
}
}
void Thread::run() {
LOG_WARN_STR("This thread is running a empty method!!");
}
void Thread::operator ()() {
do {
run();
} while (!isStoped());
}
bool Thread::isStoped() {
boost::mutex::scoped_lock lock(m_mustStopMutex);
return (m_mustStop);
}
void Thread::setMustStop(volatile bool stop) {
boost::mutex::scoped_lock lock(m_mustStopMutex);
m_mustStop = stop;
}
} // namespace