/
thread.cpp
75 lines (56 loc) · 984 Bytes
/
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/*
* thread.cpp
* MonteCarloThread
*
*/
#include "debug.h"
#include "thread.h"
void *execThread(void *arg);
Thread::Thread()
{
#ifdef DEBUG
cout << "Contructing thread...\n";
#endif
}
Thread::~Thread()
{
#ifdef DEBUG
cout << "Destructing thread...\n";
#endif
}
void Thread::launch()
{
#ifdef DEBUG
cout << "Creating pthread...\n";
#endif
int ret;
//if ((ret = pthread_create(&t_id, NULL, &Thread::exec, this)) != 0) {
if ((ret = pthread_create(&t_id, NULL, &execThread, this)) != 0) {
cout << strerror(ret) << endl;
//throw "Error creating thread!";
}
}
void Thread::join()
{
pthread_join(t_id, NULL);
}
void Thread::exit()
{
pthread_exit((void *) 0);
}
void *Thread::exec(void *t)
{
#ifdef DEBUG
cout << "Executing thread...\n";
#endif
reinterpret_cast<Thread *> (t)->run();
return NULL;
}
void *execThread(void *t)
{
#ifdef DEBUG
cout << "Executing thread part deux...\n";
#endif
reinterpret_cast<Thread *> (t)->run();
return NULL;
}