gpt4 book ai didi

c++ - 在类函数中将参数传递给 _beginthread

转载 作者:太空宇宙 更新时间:2023-11-04 14:08:38 24 4
gpt4 key购买 nike

我一直在我的 (windows) C++ 类中使用 _beginthread 来创建线程。据我了解,要使用 _beginthread 并在类中传递成员函数,必须遵循一个相当迟钝的包装协议(protocol),例如:

class MyClass
{
public:
void fun_1()
{
_beginthread(&MyClass::fun_2_wrapper, 0, static_cast<void*>(this));
}

private:
void fun_2()
{
printf("hello");
}

static void __cdecl fun_2_wrapper(void* o)
{
static_cast<MyClass*>(o)->fun_2();
}
};

我一直无法将此模型转换为接受我的线程函数参数的模型。例如,如果我希望我的线程函数改为:

void fun_2(int a)

我将如何正确调整我的函数调用? (注意,为了项目的一致性,我试图在没有 boost::thread 或类似库的情况下完成此操作)

最佳答案

您可以包装要传递给结构的参数,然后将结构传递给函数。

例子:

#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;
}

关于c++ - 在类函数中将参数传递给 _beginthread,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15845865/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com