在使用
xTaskCreate
方法将 C++ 类方法传递给 freeRTOS 任务的各种尝试之后,我所有的尝试最终都没有传递实际的类实例 - 因此 this
是空的。
此链接中显示的批准示例C++ freeRTOS Task不起作用。相反,
_this
为空,然后对成员变量的任何引用都会崩溃。
class MyClass {
public:
MyClass() {}
~MyClass() {}
private:
void update();
void task();
static void startTaskImpl(void*);
void startTask();
}
我的班级.cpp
void MyClass::task(){
while(1){
this->update();
}
}
void MyClass::startTaskImpl(void* _this){
(MyClass*)_this->task();
// _this is null (or 0)
}
void MyClass::startTask(){
xTaskCreate(this->startTaskImpl, "Task", 2048, this, 5, NULL);
}
运行时,
_this
为空,但仅在startTaskImpl
方法中。
很难测试示例,因为它需要
freeRTOS
库,我将其与 arduino
和 ESP-32
设备一起使用。
freeRTOS 表示这种包装像
startTaskImpl
这样的静态 void 方法是经过批准的方法。
(MyClass*)_this->task();
应该是
((MyClass*)_this)->task(); instead of (MyClass*)_this->task();
也就是说,在调用其方法之前将
void *
转换为 MyClass *
。