资源简介
C++封装的一个linux和windows的线程类,一个线程锁类,两个配合使用实现一份多线程的代码适应两个平台
代码片段和文件信息
// AllOSThread.cpp:
#include “AllOSThread.h“
#include
AllOSThread::AllOSThread()
{
memset(m_ThreadPoll 0 sizeof(m_ThreadPoll));
m_iCurThreadCount = 0;
}
AllOSThread::~AllOSThread()
{
}
bool AllOSThread::CreateThread(ThreadFun pFun void* pThParam)
{
if(m_iCurThreadCount >= MAX_THREAD)
return false;
#ifdef WIN32
UINT uiThreadID;
m_ThreadPoll[m_iCurThreadCount] = (THREAD_HANDLE)(::_beginthreadex(NULL NULL pFun pThParam NULL &uiThreadID));
if(NULL == m_ThreadPoll[m_iCurThreadCount])
{
assert(0);
return false;
}
m_iCurThreadCount++;
return true;
#else //linux
UINT upth;
//设置线程属性(分离)执行完后自动释放资源
pthread_attr_t attr;
pthread_attr_init (&attr);
pthread_attr_setdetachstate(&attr PTHREAD_CREATE_DETACHED);
upth = pthread_create(&m_ThreadPoll[m_iCurThreadCount]NULLpFun pThParam);
if (upth)
{
assert(0);
return false;
}
pthread_attr_destroy (&attr);
m_iCurThreadCount++;
return true;
#endif
}
void AllOSThread::DestroyThread()
{
//等待消亡线程,释放资源
#ifdef _WIN32
for(int i=0; i {
if (NULL != m_ThreadPoll[m_iCurThreadCount])
{
if(WAIT_object_0 == WaitForSingleobject(m_ThreadPoll[m_iCurThreadCount] INFINITE))
CloseHandle(m_ThreadPoll[m_iCurThreadCount]);
}
}
#else
for(int i=0; i {
pthread_join(m_ThreadPoll[m_iCurThreadCount] NULL);
}
#endif
}
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2013-12-28 19:55 ThreadLib\
文件 712 2013-12-28 19:55 ThreadLib\ReadMe.txt
目录 0 2013-05-18 00:11 ThreadLib\ThreadLib\
文件 1510 2013-05-18 00:10 ThreadLib\ThreadLib\AllOSThread.cpp
文件 1028 2013-05-18 00:10 ThreadLib\ThreadLib\AllOSThread.h
文件 380 2013-05-18 00:10 ThreadLib\ThreadLib\Makefile
文件 994 2013-05-18 00:11 ThreadLib\ThreadLib\Test.cpp
文件 733 2013-05-18 00:10 ThreadLib\ThreadLib\ThreadMutex.cpp
文件 1257 2013-05-18 00:10 ThreadLib\ThreadLib\ThreadMutex.h
- 上一篇:C++信号量实现互斥爸爸女儿儿子苹果橘子
- 下一篇:c语言实现简单数据库
评论
共有 条评论