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...

你可以使用 _beginthread 函数来创建一个新线程,并传递 recipe 参数给线程的入口函数。以下是一个示例:

#include <process.h>

class MyClass {
public:
    // 入口函数,处理 plate temp
    static unsigned __stdcall Function1(void* arg) {
        Recipe* recipe = reinterpret_cast<Recipe*>(arg);

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

        // 返回状态值(根据需要修改返回类型)
        return 0;
    }

    // 入口函数,处理 cover temp
    static unsigned __stdcall Function2(void* arg) {
        Recipe* recipe = reinterpret_cast<Recipe*>(arg);

        printf("set cover temp thread start\n");
        if (SYS_ABORTED == RUN_FUNCTIONF(Temp_Ctrl, "set-cover %.2f %.2f %.2f", recipe->GetSingleStep().coverTarget, recipe->GetSingleStep().coverAlarm, recipe->GetSingleStep().coverStop)) {
            return Global::ModuleStatus(SYS_ABORTED);
        }

        // 返回状态值(根据需要修改返回类型)
        return 0;
    }

    void YourFunction() {
        Recipe param;  // 根据需求初始化参数

        uintptr_t threadHandle1 = _beginthreadex(nullptr, 0, &MyClass::Function1, &param, 0, nullptr);
        
        uintptr_t threadHandle2 = _beginthreadex(nullptr, 0, &MyClass::Function2, &param, 0, nullptr);

        // 可以根据需求进行处理,如等待线程结束或其他操作
    }
};

在上面的示例中,我们使用了 uintptr_t _beginthreadex(void* security, unsigned stack_size, unsigned(__stdcall* start_address)(void*), void* arglist, unsigned initflag, unsigned* thrdaddr) 函数来创建新线程。注意这里使用 _beginthreadex 而不是 _beginthread,因为 _beginthreadex 在 Windows 平台上更常用,并且支持返回线程句柄。

YourFunction() 中,我们创建了两个新线程,分别调用 MyClass::Function1MyClass::Function2 作为入口函数,并将参数 param 传递给这两个函数。

请注意,在跨线程中共享对象时要小心处理同步问题和资源管理。此外,还需要根据具体情况修改返回值类型、处理错误和添加必要的同步机制。


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

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

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

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

联系我们

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

微信号:3007537140

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

关注我们
x

注册

已经有帐号?