要将 recipe
作为参数传递给 _beginthread
创建的线程,你可以使用一个中间函数来包装成员函数,并将对象指针和 recipe
对象作为参数传递给中间函数。以下是修改后的示例代码:
#include <process.h> // 包含 _beginthread 函数声明
class MyClass {
public:
struct ThreadData {
Recipe* recipe;
MyClass* obj;
};
// 中间函数
static unsigned int __stdcall threadFunc(void* param) {
ThreadData* data = static_cast<ThreadData*>(param);
Recipe& recipe = *(data->recipe);
MyClass& obj = *(data->obj);
obj.function1(recipe); // 调用成员函数 function1
delete data; // 释放动态分配的内存
return 0;
}
void startThread(Recipe& recipe) {
ThreadData* data = new ThreadData();
data->recipe = &recipe;
data->obj = this;
_beginthreadex(NULL, 0, &threadFunc, data, 0, NULL);
}
private:
Module_Status function1(Recipe& recipe) {
// ...
return Global::ModuleStatus(SYS_ABORTED);
}
// 其他成员函数省略...
};
在上述示例中,我们创建了一个新的结构体 ThreadData
,其中包含了指向 Recipe
对象和 MyClass
对象的指针。然后,在 startThread
函数中,我们动态分配了一个 ThreadData
对象,并将其作为参数传递给 _beginthreadex
函数。在 threadFunc
中,我们从 ThreadData
对象中获取指针,并使用它们来调用相应的成员函数。
请注意,在你的实际代码中,你可能需要根据需要修改函数的返回类型、参数类型和具体实现,以使其适合你的需求。
内容由零声教学AI助手提供,问题来源于学员提问