You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Zephyr RTOS中C++类线程访问成员变量的优化实现问询

问题

我深知当前实现既非线程安全也不够简洁,因此提出此问题:我试图在Zephyr RTOS的C类中实现可访问成员变量的线程,这本质是一个通用C问题。以下是我目前可用的实现方案:

stepper.h

class Stepper
{
...
private:
    double speed;

    struct k_thread stepper_thread;

    /* Stepper thread entry */
    static void stepper_thread_entry(void *, void *, void *);

    /* Stepper thread function */
    void stepper_thread_function(auto& desired_speed);
};

stepper.cpp

Stepper::Stepper(void) : speed(0)
{
k_tid_t stepper_thread_tid = k_thread_create(&stepper_thread,    
                                  stepper_thread_stack_area,
                                  K_THREAD_STACK_SIZEOF(stepper_thread_stack_area),                                
                                  Stepper::stepper_thread_entry, 
                                  this, NULL, NULL,
                                  STEPPER_THREAD_PRIORITY, 0, K_MSEC(10));

}

...

void Stepper::stepper_thread_entry(void *instance, void *, void *)
{
    auto thead_instance = reinterpret_cast<Stepper *>(instance);
    thead_instance->stepper_thread_function(thead_instance->speed);
}

void Stepper::stepper_thread_function(double& desired_speed)
{
    // access desired_speed
    ...
}

我对Stepper::stepper_thread_entry的实现并不满意,但这是我找到的唯一能规避K_THREAD_STACK_SIZEOF宏要求静态函数限制的方法。请问是否有更简洁的实现方式?


优化实现方案

可以通过简化线程入口逻辑、直接在成员函数中访问类成员的方式,让代码更简洁:

优化后的stepper.h

class Stepper
{
...
private:
    double speed;
    struct k_thread stepper_thread;

    // 静态线程入口函数
    static void stepper_thread_entry(void *instance, void *, void *);
    // 线程工作循环,直接访问类成员
    void stepper_thread_loop();
};

优化后的stepper.cpp

Stepper::Stepper(void) : speed(0)
{
    k_tid_t stepper_thread_tid = k_thread_create(
        &stepper_thread,
        stepper_thread_stack_area,
        K_THREAD_STACK_SIZEOF(stepper_thread_stack_area),
        Stepper::stepper_thread_entry,
        this, NULL, NULL,
        STEPPER_THREAD_PRIORITY, 0, K_MSEC(10)
    );
}

// 简化的静态入口,仅做类型转换
void Stepper::stepper_thread_entry(void *instance, void *, void *)
{
    static_cast<Stepper*>(instance)->stepper_thread_loop();
}

// 直接访问类成员的工作函数
void Stepper::stepper_thread_loop()
{
    while (true) {
        // 直接使用this->speed,无需额外传递参数
        // 示例逻辑:根据speed执行步进动作
        ...
        k_msleep(10); // 按需调整线程调度延时
    }
}

额外的线程安全优化

如果需要保证speed的线程安全,可以在类中添加Zephyr互斥锁:

// stepper.h中添加互斥锁成员
private:
    struct k_mutex speed_mutex;

// 构造函数中初始化互斥锁
Stepper::Stepper(void) : speed(0)
{
    k_mutex_init(&speed_mutex);
    // 线程创建代码不变
}

// 访问speed时加锁
void Stepper::stepper_thread_loop()
{
    while (true) {
        k_mutex_lock(&speed_mutex, K_FOREVER);
        double current_speed = speed;
        k_mutex_unlock(&speed_mutex);
        
        // 使用current_speed处理业务逻辑
        ...
        k_msleep(10);
    }
}

内容的提问来源于stack exchange,提问作者juri

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 14:05:15