如何用类的非静态成员函数作为线程回调函数?求
怎么用类的非静态成员函数作为线程回调函数?求高手指点
我的代码是这样的
#include "windows.h"
#include <process.h>
#include "iostream"
using namespace std;
class Test{
public:
static DWORD WINAPI mThread(PVOID lpParam);
void ThreadPROC(void* pParam);
int CallThread();
private:
static int i;
int j;
};
int Test::i = 0;
DWORD Test::mThread(PVOID pParam)
{
Test* p = (Test*)pParam;
p->i = 1;
OutputDebugString(L"线程执行\n");
return 0;
}
void Test::ThreadPROC(void* pParam){}
int Test::CallThread()
{
//使用静态成员函数作为线程函数
DWORD ThreadID = 1;
HANDLE hThread = CreateThread(0 , 0, &Test::mThread, this, 0, &ThreadID);
if (hThread == NULL)
{
MessageBox(NULL, L"线程创建失败", NULL, NULL);
}
//不使用静态成员函数作为线程函数
union{
void (_USERENTRY * ThreadProc)(void*);
void(_USERENTRY Test::*MemoryProc)(void*);
}proc;
proc.MemoryProc = &Test::ThreadPROC;
_beginthread(proc.ThreadProc, 0, this);
return 0;
}
int main()
{
Test t;
t.CallThread();
while (1)
{
}
return 0;
}
_USERENTRY 也不知道是什么东西,在网上查不到
编译后好多错误
------解决方案--------------------
我的代码是这样的
#include "windows.h"
#include <process.h>
#include "iostream"
using namespace std;
class Test{
public:
static DWORD WINAPI mThread(PVOID lpParam);
void ThreadPROC(void* pParam);
int CallThread();
private:
static int i;
int j;
};
int Test::i = 0;
DWORD Test::mThread(PVOID pParam)
{
Test* p = (Test*)pParam;
p->i = 1;
OutputDebugString(L"线程执行\n");
return 0;
}
void Test::ThreadPROC(void* pParam){}
int Test::CallThread()
{
//使用静态成员函数作为线程函数
DWORD ThreadID = 1;
HANDLE hThread = CreateThread(0 , 0, &Test::mThread, this, 0, &ThreadID);
if (hThread == NULL)
{
MessageBox(NULL, L"线程创建失败", NULL, NULL);
}
//不使用静态成员函数作为线程函数
union{
void (_USERENTRY * ThreadProc)(void*);
void(_USERENTRY Test::*MemoryProc)(void*);
}proc;
proc.MemoryProc = &Test::ThreadPROC;
_beginthread(proc.ThreadProc, 0, this);
return 0;
}
int main()
{
Test t;
t.CallThread();
while (1)
{
}
return 0;
}
_USERENTRY 也不知道是什么东西,在网上查不到
编译后好多错误
线程
类
------解决方案--------------------
#ifndef _XTHREAD_INCLUDED_
#define _XTHREAD_INCLUDED_
#if defined(WIN32)
#include <windows.h>
#define DEFAULT_STACK_SIZE 0
#define DEFAULT_THREAD_PRIORITY 0
#define MS_VC_EXCEPTION 0x406D1388
typedef struct tagTHREADNAME_INFO
{
DWORD dwType; // Must be 0x1000.
LPCSTR szName; // Pointer to name (in user addr space).
DWORD dwThreadID; // Thread ID (-1=caller thread).
DWORD dwFlags; // Reserved for future use, must be zero.
} THREADNAME_INFO;
class CThread //线程类
{
private:
HANDLE m_hThread; //线程句柄
int m_nThreadPriority;
DWORD m_dwThreadStackSize;
private:
static DWORD WINAPI ThreadBody(LPVOID pParam)
{
if (!pParam)
return DWORD(-1);
CThread *pThis = (CThread *)pParam;
pThis->SetThreadName(-1, "win-thread");
return pThis ->Run();
}
void SetThreadName( DWORD dwThreadID, LPCSTR szThreadName)
{
THREADNAME_INFO info;
info.dwType = 0x1000;
info.szName = szThreadName;
info.dwThreadID = dwThreadID;
info.dwFlags = 0;
__try
{
RaiseException( MS_VC_EXCEPTION, 0, sizeof(info)/sizeof(DWORD), (DWORD*)&info );
}
__except(EXCEPTION_CONTINUE_EXECUTION)
{
}
}
protected:
virtual DWORD Run() = 0;
public:
CThread(HANDLE hThread=NULL,
int nThreadPriority=DEFAULT_THREAD_PRIORITY,
DWORD dwThreadStackSize=DEFAULT_STACK_SIZE)
:m_hThread(hThread), m_nThreadPriority(nThreadPriority), m_dwThreadStackSize(dwThreadStackSize){};
private:
// 禁止拷贝构造函数。
CThread(const CThread&);
// 禁止赋值操作符。
void operator=(const CThread&);
public:
BOOL Start() //创建线程并启动
{
if (m_hThread)
return FALSE;
m_hThread = ::CreateThread(NULL, m_dwThreadStackSize, (LPTHREAD_START_ROUTINE)CThread::ThreadBody, (LPVOID)this, 0, NULL);
if (NULL != m_hThread)
return FALSE;
if (abs(m_nThreadPriority) > THREAD_BASE_PRIORITY_MAX && m_nThreadPriority != THREAD_PRIORITY_TIME_CRITICAL)
{
m_nThreadPriority = 0;
}
::SetThreadPriority(m_hThread, m_nThreadPriority);
return TRUE;
}
void Join() //等待线程退出
{
::WaitForSingleObject(m_hThread,INFINITE);
}
DWORD SuspendThread()
{
return ::SuspendThread(m_hThread);
}
DWORD ResumeThread()