-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy paththread.cpp
67 lines (47 loc) · 1.04 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
59
60
61
62
63
64
65
66
67
/*
* thread.cpp
*
* Created on: 2011-10-26
* Author: gxl2007@hotmail.com
*/
#include <pthread.h>
#include <signal.h>
#include "thread.h"
namespace xlnet
{
int thread::start()
{
if ( m_tid != 0 ) return -1 ;
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setstacksize(&attr, 8 << 20 );
sigset_t sigmask ;
sigfillset(&sigmask) ;
pthread_sigmask(SIG_BLOCK,&sigmask,NULL) ;
int ret = pthread_create((pthread_t*)&m_tid, &attr, thread_entry, this);
pthread_sigmask(SIG_UNBLOCK,&sigmask,NULL) ;
pthread_attr_destroy(&attr) ;
return ret ;
}
void thread::join()
{
if ( m_tid != 0 )
{
pthread_join(m_tid,NULL) ;
m_tid = 0 ;
}
}
void* thread::thread_entry (void* arg)
{
thread* cur_thread = (thread*)arg;
cur_thread->run() ;
return NULL ;
}
void simple_thread::run()
{
m_status = 1 ;
if( on_init() != 0 ) return ;
while(m_status == 1) run_once() ;
on_fini() ;
}
}