ZBLOG

class MyClass { public: //set plate temp Module_Status function1(Recipe &recipe) { printf("set plate temp thread start\n"); if (SYS_ABORTED == RUN_FUNCTIONF(Temp_Ctrl, "set-plate %.2f %.2f %.2f", recipe.GetSingleStep().plat...

你可以使用一个中间函数来包装成员函数,并将对象指针和 recipe 对象作为参数传递给中间函数。以下是示例代码:

class MyClass {
public:
    struct ThreadData {
        Recipe* recipe;
        MyClass* obj;
    };

    // 中间函数,用于调用成员函数 function1
    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) {
       printf("set plate temp thread start\n");
       if (SYS_ABORTED == RUN_FUNCTIONF(Temp_Ctrl, "set-plate %.2f %.2f %.2f", recipe.GetSingleStep().plateTarget, recipe.GetSingleStep().plateAlarm, recipe.GetSingleStep().plateStop))
       {
           return Global::ModuleStatus(SYS_ABORTED);
       }
       _endthread();
   }

   // 其他成员函数省略...
};

在上述示例中,我们创建了一个新的结构体 ThreadData,其中包含了指向 Recipe 对象和 MyClass 对象的指针。然后,在 startThread 函数中,我们动态分配了一个 ThreadData 对象,并将其作为参数传递给 _beginthreadex 函数。在 threadFunc 中,我们从 ThreadData 对象中获取指针,并使用它们来调用相应的成员函数。

main 函数中,你可以这样使用:

int main() {
    Recipe param; // 假设有一个 Recipe 参数

    MyClass myObj;
    myObj.startThread(param);

    // 其他代码...
}

请注意,根据你的实际需求和代码结构,你可能需要进行适当的修改。


内容由零声教学AI助手提供,问题来源于学员提问

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:https://sdn.0voice.com/?id=4158

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?