-
Notifications
You must be signed in to change notification settings - Fork 0
/
thread.cpp
58 lines (49 loc) · 1.31 KB
/
thread.cpp
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
/*
* File: thread.cpp
* Group: 14
* Description: Implementation for the Thread abstract clas.
*/
#include "thread.h"
#include "util.h"
Thread::Thread() : _cancelFlag(false) {
Log::info("Thread constructor()");
pthread_mutex_init(&_cancelFlagMutex, NULL);
}
/*
* Creates a new thread and starts execution in the new thread. Returns true if
* the thread was started successfully.
*/
bool Thread::startThread() {
pthread_attr_t attr;
pthread_attr_init (&attr);
pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
bool result = (pthread_create(&_threadId, NULL, &executeRun, this) == 0);
pthread_attr_destroy (&attr);
return result;
}
/*
* Sets the cacel flag to true;
*/
void Thread::stopThread() {
Log::info("in stopThread()");
pthread_mutex_lock(&_cancelFlagMutex);
_cancelFlag = true;
pthread_mutex_unlock(&_cancelFlagMutex);
}
bool Thread::isCancelFlagSet() {
bool result = false;
pthread_mutex_lock(&_cancelFlagMutex);
result = _cancelFlag;
pthread_mutex_unlock(&_cancelFlagMutex);
return result;
}
/*
* Wrapper for the run() function which gets implemented by the derived class.
* Allows the new thread to still be able to access class members
*
* context: Pointer to the class calling start()
*/
void* Thread::executeRun(void* context) {
((Thread*)context)->run();
return NULL;
}