-
Notifications
You must be signed in to change notification settings - Fork 110
/
Copy pathThreadable.cpp
executable file
·85 lines (71 loc) · 1.5 KB
/
Threadable.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
#include "Threadable.h"
#include "LibeventLog.h"
#ifndef WIN32
#include <pthread.h>
#include <unistd.h>
#else
#include <windows.h>
#endif
#include <stddef.h>
#include <iostream>
using namespace std;
Threadable::Threadable(void)
{
_threadId = -1;
}
Threadable::~Threadable(void)
{
}
static void* ThreadFunc(void *args) {
Threadable *self = (Threadable *)args;
self->run();
return NULL;
}
int Threadable::yield() {
#ifndef WIN32
return sched_yield();
#else
SwitchToThread();
#endif
return 0;
}
static int startThread(void* (*threadFun) (void *), void *args) {
#ifndef WIN32
int hThread;
pthread_t recvthread;
hThread = pthread_create(&recvthread, NULL, threadFun, args);
if (hThread != 0) {
LibeventLog::userLog("pthread create failed.");
return -1;
} else {
pthread_detach(recvthread);
}
return recvthread;
#else
DWORD ThreadId;
DWORD dwThreadId;
ThreadId = (int)CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)threadFun, args, 0, &dwThreadId);
if (ThreadId == -1) {
return -1;
}
SetThreadPriority((HANDLE)ThreadId,THREAD_PRIORITY_NORMAL);
return ThreadId;
#endif
}
void Threadable::start() {
//create thread
_threadId = startThread(ThreadFunc, this);
}
void Threadable::join() {
#ifndef WIN32
if (_threadId > 0)
pthread_join(_threadId, NULL);
#endif
}
void Threadable::milliSleep(unsigned long milli) {
#ifndef WIN32
usleep(milli*1000);
#else
Sleep(milli);
#endif
}