您可以包装将传递给结构的参数,然后将结构传递给函数。
例子:
#include <Windows.h>
#include "stdio.h"
#include "stdlib.h"
#include <process.h>
class TestWrapperMultThreadToClass
{
public:
TestWrapperMultThreadToClass(int ThreadNum)
{
_ThreadNum = ThreadNum; // how many thread we would create
TestTimes = ThreadNum * 2;
}
~TestWrapperMultThreadToClass()
{
}
bool TestMultThreadSendParam()
{
tb = new ThreadBlockParam[_ThreadNum];
for (int i = 0; i < _ThreadNum; i++) // initial
(tb + i)->IsIdle = true;
int ThreadIndex = 0, nowThread = 0;
unsigned int ThreadId_For_Beginthreadex_Function = 0;
for (int i = 0; i < TestTimes; i++)
{
ThreadId_For_Beginthreadex_Function = static_cast<unsigned int>(i);
nowThread = i;
// wrap arguments
(tb + nowThread)->ThreadIndex = ThreadId_For_Beginthreadex_Function;
(tb + nowThread)->InIterativeTimes = i;
(tb + nowThread)->IsIdle = false;
(tb + nowThread)->_wrap = (void*) this;
_beginthreadex(NULL, 0, FunctionCalledByThread, (tb + nowThread), 0, &ThreadId_For_Beginthreadex_Function);
}
IsAllThreadComeBack(tb, _ThreadNum); // check whether all threads come back
return true;
}
private:
int _ThreadNum, TestTimes;
typedef struct _ThreadBlockParam
{
int ThreadIndex;
int InIterativeTimes;
bool IsIdle;
void* _wrap;
}ThreadBlockParam;
ThreadBlockParam *tb;
static unsigned int __stdcall FunctionCalledByThread(void *ptr_this)
{
ThreadBlockParam *_tb = static_cast<ThreadBlockParam*>(ptr_this);
TestWrapperMultThreadToClass *_this = (TestWrapperMultThreadToClass*)(_tb->_wrap);
_this->Test(_tb);
return 1;
}
void Test(ThreadBlockParam* _tb)
{
ThreadBlockParam *temp = _tb;
int ThreadId = GetCurrentThreadId();
char Msg[200];
Sleep(1000);
sprintf(Msg, "In %d-th thread, thread id = %d!\n", temp->ThreadIndex, ThreadId);
printf(Msg);
Sleep(5000); // wait few second to check whether execute with different thread.
temp->IsIdle = true;
}
bool IsAllThreadComeBack(ThreadBlockParam *tb, int ThreadNum)
{
int count = 0;
while (count < ThreadNum)
{
if ((tb + count)->IsIdle == true)
count++;
}
return true;
}
};
int main()
{
TestWrapperMultThreadToClass *Test = new TestWrapperMultThreadToClass(8);
Test->TestMultThreadSendParam();
printf("Multiple thread wrapper to class testing is finishing!\n");
system("pause");
return 0;
}