一段 C++ 代码(成员函数做线程函数)
2014-07-01 09:07
399 查看
#include 〈windows.h〉
#include 〈conio.h〉
#include 〈stdio.h〉
class CThreadClass
{
public:
typedef DWORD (WINAPI CThreadClass::* pMemberThreadFunc)();
typedef DWORD (WINAPI * PTHREADFUNC)(LPVOID);
CThreadClass();
void SetDescrip(char* str) { lstrcpy(m_str,str); }
void PrintfDescrip() { printf(“m_str = %s\n“,m_str); }
void RunThread();
bool FinishThread();
protected:
char m_str[1024];
DWORD WINAPI ThreadFunc();
HANDLE m_ThreadHandle;
DWORD m_ThreadID;
pMemberThreadFunc m_pMemberFun;
};
CThreadClass::CThreadClass()
{
memset(m_str,0,1024);
m_ThreadID = 0;
m_ThreadHandle = 0;
m_pMemberFun = ThreadFunc;
}
void CThreadClass::RunThread()
{
PTHREADFUNC pFun;
DWORD dwAddress;
memcpy(&dwAddress,&m_pMemberFun,sizeof(m_pMemberFun));
memcpy(&pFun, &dwAddress, sizeof(pFun));
m_ThreadHandle = CreateThread(NULL, 0, pFun, this, 0, &m_ThreadID);
}
bool CThreadClass::FinishThread()
{
bool bRet = true;
if (m_ThreadID == NULL)
return bRet;
::PostThreadMessage(m_ThreadID, WM_QUIT, 0, 0);
DWORD rc = ::WaitForSingleObject(m_ThreadHandle, 5000);
if(rc == WAIT_TIMEOUT)
{
TerminateThread(m_ThreadHandle,0);
bRet = false;
}
if (m_ThreadHandle)
CloseHandle(m_ThreadHandle);
m_ThreadHandle = NULL;
m_ThreadID = 0;
return bRet;
}
DWORD WINAPI CThreadClass::ThreadFunc()
{
for(;;)
{
PrintfDescrip();
Sleep(3000);
}
return 0;
}
void main()
{
CThreadClass t1, t2;
t1.SetDescrip(“the t1 Member Function Thread run!“);
t1.RunThread();
t2.SetDescrip(“the t2 Member Function Thread run!“);
t2.RunThread();
getch();
printf(“wait for TerminateThread...\n“);
t1.FinishThread();
t2.FinishThread();
printf(“exit!\n“);
getch();
}
#include 〈conio.h〉
#include 〈stdio.h〉
class CThreadClass
{
public:
typedef DWORD (WINAPI CThreadClass::* pMemberThreadFunc)();
typedef DWORD (WINAPI * PTHREADFUNC)(LPVOID);
CThreadClass();
void SetDescrip(char* str) { lstrcpy(m_str,str); }
void PrintfDescrip() { printf(“m_str = %s\n“,m_str); }
void RunThread();
bool FinishThread();
protected:
char m_str[1024];
DWORD WINAPI ThreadFunc();
HANDLE m_ThreadHandle;
DWORD m_ThreadID;
pMemberThreadFunc m_pMemberFun;
};
CThreadClass::CThreadClass()
{
memset(m_str,0,1024);
m_ThreadID = 0;
m_ThreadHandle = 0;
m_pMemberFun = ThreadFunc;
}
void CThreadClass::RunThread()
{
PTHREADFUNC pFun;
DWORD dwAddress;
memcpy(&dwAddress,&m_pMemberFun,sizeof(m_pMemberFun));
memcpy(&pFun, &dwAddress, sizeof(pFun));
m_ThreadHandle = CreateThread(NULL, 0, pFun, this, 0, &m_ThreadID);
}
bool CThreadClass::FinishThread()
{
bool bRet = true;
if (m_ThreadID == NULL)
return bRet;
::PostThreadMessage(m_ThreadID, WM_QUIT, 0, 0);
DWORD rc = ::WaitForSingleObject(m_ThreadHandle, 5000);
if(rc == WAIT_TIMEOUT)
{
TerminateThread(m_ThreadHandle,0);
bRet = false;
}
if (m_ThreadHandle)
CloseHandle(m_ThreadHandle);
m_ThreadHandle = NULL;
m_ThreadID = 0;
return bRet;
}
DWORD WINAPI CThreadClass::ThreadFunc()
{
for(;;)
{
PrintfDescrip();
Sleep(3000);
}
return 0;
}
void main()
{
CThreadClass t1, t2;
t1.SetDescrip(“the t1 Member Function Thread run!“);
t1.RunThread();
t2.SetDescrip(“the t2 Member Function Thread run!“);
t2.RunThread();
getch();
printf(“wait for TerminateThread...\n“);
t1.FinishThread();
t2.FinishThread();
printf(“exit!\n“);
getch();
}
相关文章推荐
- C++中的线程函数如何访问类中的成员变量
- C++ - 成员函数(member function)模板(template) 详解 及 代码
- C++中 线程函数为静态函数 及 类成员函数作为回调函数
- 将类成员函数作为线程入口函数——关于C++ 类静态成员的学习
- 为什么在C++使用pthread_create()的时候,类成员函数做线程的处理函数必须要定义成static类型的?
- C++中线程函数中方访问成员变量的问题
- C++中的类所占内存空间总结(其中有一段关于成员函数处于代码段的解释) 2011-12-9 16:16
- C++中类的成员函数作为 pthread_create的线程函数
- 为什么在C++使用pthread_create()的时候,类成员函数做线程的处理函数必须要定义成static类型的?
- 在C++里一个类成员函数多少行代码才是最好呢?
- 不使用汇编在vc中转换类的成员函数为一个线程的通用代码
- C++中的类所占内存空间总结(其中有一段关于成员函数处于代码段的解释)
- C++中线程函数中方访问成员变量
- c++ public, protected, private成员变量,成员函数继承和访问规则实例代码
- C++ 11 线程调用类的成员函数解决办法
- C++成员函数如何调用线程
- C++中 线程函数为静态函数 及 类成员函数作为回调函数
- C++ 类非静态成员函数作为线程入口函数实现
- C++创建线程问题 一段可用代码
- C++中的线程函数如何访问类中的成员变量