-
Notifications
You must be signed in to change notification settings - Fork 0
/
ThreadWrapper.cpp
104 lines (88 loc) · 1.83 KB
/
ThreadWrapper.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include <iostream>
#include <thread>
class ThreadWrapper
{
// initialize n number of threads with function
std::thread *threads;
int threadCount;
int count = 0;
void (*function)(int);
public:
ThreadWrapper(int threadCount)
{
this->threadCount = threadCount;
this->function = NULL;
threads = new std::thread[threadCount];
}
ThreadWrapper(int threadCount, void (*function)(int))
{
this->threadCount = threadCount;
this->function = function;
threads = new std::thread[threadCount];
}
void start()
{
this->count = this->threadCount;
for (int i = 0; i < threadCount; i++)
{
threads[i] = std::thread(function, i);
}
}
void start(void (*function)(int))
{
threads[i] = std::thread(function, i);
this->count = i;
int i = this->count + 1;
}
void join()
{
for (int i = 0; i < threadCount; i++)
{
threads[i].join();
}
}
void stop()
{
for (int i = 0; i < threadCount; i++)
{
threads[i].detach();
}
}
void destroy()
{
delete[] threads;
}
int getThreadCount()
{
return threadCount;
}
int getThreadId(int index)
{
return threads[index].get_id();
}
int getThreadStatus(int index)
{
return threads[index].joinable();
}
int getThreadPriority(int index)
{
return threads[index].get_priority();
}
void setThreadPriority(int index, int priority)
{
threads[index].set_priority(priority);
}
};
// driver code
void hello(int i)
{
std::cout << "Hello " << i << std::endl;
}
int main()
{
ThreadWrapper tw(1);
tw.start(hello);
tw.join();
tw.destroy();
return 0;
}