-
Notifications
You must be signed in to change notification settings - Fork 0
/
t_thread.cpp
70 lines (61 loc) · 1.5 KB
/
t_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
68
69
70
#include "t_thread.hpp"
namespace T_TCP
{
PthreadBase::PthreadBase() :m_iPid(-1), m_bRun(false),
m_pInitLock(NULL), m_pInitCond(NULL),
m_pReadyThreadNums(NULL)
{
}
PthreadBase::PthreadBase(pthread_mutex_t* pInitLock, pthread_cond_t* pInitCond, int* pInitNums)
:m_iPid(-1), m_bRun(false),m_pInitLock(pInitLock), m_pInitCond(pInitCond),
m_pReadyThreadNums(pInitNums)
{
}
PthreadBase::~PthreadBase()
{
}
void PthreadBase::Start()
{
int iRet = pthread_create(&m_iPid, NULL, Entry, this);
if (iRet != 0)
{
return ;
}
}
void PthreadBase::Stop()
{
m_bRun = false;
}
void* PthreadBase::Entry(void* pData)
{
PthreadBase* pThis = (PthreadBase*)pData;
if (pThis == NULL)
{
return NULL;
}
if (pThis->Init() < 0)
{
return NULL;
}
pThis->SetRun();
while(pThis->IsRun())
{
pThis->main();
}
}
void PthreadBase::JoinWork()
{
pthread_join(m_iPid, NULL);
}
void PthreadBase::RegistePthreadToPool()
{
if (m_pInitLock == NULL || m_pInitCond == NULL || m_pReadyThreadNums == NULL)
{
return ;
}
pthread_mutex_lock(m_pInitLock);
++(*m_pReadyThreadNums);
pthread_cond_signal(m_pInitCond);
pthread_mutex_unlock(m_pInitLock);
}
}