簡體   English   中英

C++ freeRTOS 任務,非靜態成員函數的無效使用

[英]C++ freeRTOS Task, invalid use of non-static member function

問題出在哪里?

void MyClass::task(void *pvParameter){
     while(1){
         this->update();
     }
}

void MyClass::startTask(){
    xTaskCreate(this->task, "Task", 2048, NULL, 5, NULL);
}

但是,我明白了:

錯誤:無效使用非靜態成員函數

我找不到任何有用的文檔來檢查錯誤在哪里,
但我認為應該是這樣的:(C++11 的 std::thread)例如:

xTaskCreate(&MyClass::task, "Task", 2048, (void*)this, 5, NULL);

適合我的解決方案:

void MyClass::task(){
    while(1){
        this->update();
    }
}

static void MyClass::startTaskImpl(void* _this){
    static_cast<MyClass*>(_this)->task();
}

void MyClass::startTask(){
    xTaskCreate(this->startTaskImpl, "Task", 2048, this, 5, NULL);
}

我將此模式與包裝函數一起使用,以使用非靜態成員函數實例化 pthread。 xTask中調用的函數是靜態成員函數,使用void*指針調用任務函數。 我的類.hpp:

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();
}
void MyClass::startTask(){
    xTaskCreate(this->startTaskImpl, "Task", 2048, this, 5, NULL);
}

根據這個 FreeRTOS 官方線程,您可以編寫包裝函數來實現這一點。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM