-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathThread.cc
48 lines (47 loc) · 875 Bytes
/
Thread.cc
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
//
///
/// @file Thread.cc
/// @author yll(1711019653@qq.com)
/// @date 2019-02-10 22:07:27
///
#include "Thread.h"
#include <pthread.h>
namespace yll
{
Thread::Thread(callback && callbackThread)
:_callbackThread(std::move(callbackThread))//转换为临时变量
,_pthid(0)
,_isRunning(false)
{}
void Thread::start()
{
pthread_create(&_pthid, NULL, threadFunc, this);
_isRunning = true;
}
void * Thread::threadFunc(void * arg)
{
//得到arg的值为this指针的值,需要将其类型转换
Thread * p = static_cast<Thread *>(arg);
if(p){
p->_callbackThread();
p->_isRunning = true;
}
return NULL;
}
void Thread::join()
{
if(_isRunning)
{
pthread_join(_pthid, NULL);
_isRunning = false;
}
}
Thread::~Thread()
{
if(_isRunning)
{
pthread_detach(_pthid);
_isRunning = false;
}
}
}// end of namespace yll