-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathThread.h
More file actions
141 lines (133 loc) · 3.36 KB
/
Copy pathThread.h
File metadata and controls
141 lines (133 loc) · 3.36 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
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#pragma once
#include <unistd.h>
#include <pthread.h>
#include <fcntl.h>
#include <signal.h>
#include <map>
#include "Function.h"
class CThread
{
public:
CThread() {
m_function = NULL;
m_thread = 0;
m_bpaused = false;
}
template<typename _FUNCTION_, typename... _ARGS_>
CThread(_FUNCTION_ func, _ARGS_... args)
:m_function(new CFunction<_FUNCTION_, _ARGS_...>(func, args...))
{
m_thread = 0;
m_bpaused = false;
}
~CThread() {}
public:
CThread(const CThread&) = delete;
CThread operator=(const CThread&) = delete;
public:
template<typename _FUNCTION_, typename... _ARGS_>
int SetThreadFunc(_FUNCTION_ func, _ARGS_... args)
{
m_function = new CFunction<_FUNCTION_, _ARGS_...>(func, args...);
if (m_function == NULL)return -1;
return 0;
}
int Start() {
pthread_attr_t attr;
int ret = 0;
ret = pthread_attr_init(&attr);
if (ret != 0)return -1;
ret = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
if (ret != 0)return -2;
//ret = pthread_attr_setscope(&attr, PTHREAD_SCOPE_PROCESS);
//if (ret != 0)return -3;
ret = pthread_create(&m_thread, &attr, &CThread::ThreadEntry, this);
if (ret != 0)return -4;
m_mapThread[m_thread] = this;
ret = pthread_attr_destroy(&attr);
if (ret != 0)return -5;
return 0;
}
int Pause() {
if (m_thread != 0)return -1;
if (m_bpaused) {
m_bpaused = false;
return 0;
}
m_bpaused = true;
int ret = pthread_kill(m_thread, SIGUSR1);
if (ret != 0) {
m_bpaused = false;
return -2;
}
return 0;
}
int Stop() {
if (m_thread != 0) {
pthread_t thread = m_thread;
m_thread = 0;
timespec ts;
ts.tv_sec = 0;
ts.tv_nsec = 100 * 1000000;//100ms
int ret = pthread_timedjoin_np(thread, NULL, &ts);
if (ret == ETIMEDOUT) {
pthread_detach(thread);
pthread_kill(thread, SIGUSR2);
}
}
return 0;
}
bool isValid()const { return m_thread != 0; }
private:
//__stdcall
static void* ThreadEntry(void* arg) {
CThread* thiz = (CThread*)arg;
struct sigaction act = { 0 };
sigemptyset(&act.sa_mask);
act.sa_flags = SA_SIGINFO;
act.sa_sigaction = &CThread::Sigaction;
sigaction(SIGUSR1, &act, NULL);
sigaction(SIGUSR2, &act, NULL);
thiz->EnterThread();
if (thiz->m_thread)thiz->m_thread = 0;
pthread_t thread = pthread_self();//不是冗余,有可能被stop函数把m_thread给清零了
auto it = m_mapThread.find(thread);
if (it != m_mapThread.end())
m_mapThread[thread] = NULL;
pthread_detach(thread);
pthread_exit(NULL);
}
static void Sigaction(int signo, siginfo_t* info, void* context)
{
if (signo == SIGUSR1) {
pthread_t thread = pthread_self();
auto it = m_mapThread.find(thread);
if (it != m_mapThread.end()) {
if (it->second) {
while (it->second->m_bpaused) {
if (it->second->m_thread == 0) {
pthread_exit(NULL);
}
usleep(1000);//1ms
}
}
}
}
else if (signo == SIGUSR2) {//线程退出
pthread_exit(NULL);
}
}
void EnterThread() {//__thiscall
if (m_function != NULL) {
int ret = (*m_function)();
if (ret != 0) {
printf("%s(%d):[%s]ret = %d\n", __FILE__, __LINE__, __FUNCTION__);
}
}
}
private:
CFunctionBase* m_function;
pthread_t m_thread;
bool m_bpaused;//true 表示暂停 false表示运行中
static std::map<pthread_t, CThread*> m_mapThread;
};