我试图在一个类中使用RtosTimer但是mbed会锁定。我想这是因为我调用threadHelper每个tick并创建一个新指针,而我实际上想调用threadMethod每个tick或每次tick都调用threadHeper但使用相同的指针。
有人能告诉我应该怎么做吗?
下面的代码适用于RtosThread,因为threadHelper只调用一次,但我需要使用Rtos Timer。
·H
#ifndef TEST_CLASS_H
#define TEST_CLASS_H
#include "mbed.h"
#include "rtos.h"
/** TestClass class.
* Used for demonstrating stuff.
*/
class TestClass
{
public:
/** Create a TestClass object with the specified specifics
*
* @param led The LED pin.
* @param flashRate The rate to flash the LED at in Hz.
*/
TestClass(PinName led, float flashRate);
/** Start flashing the LED using a Thread
*/
void start();
private:
//Member variables
DigitalOut m_Led;
float m_FlashRate;
RtosTimer *m_rtosTimer;
//Internal methods
static void threadHelper(const void* arg);
void threadMethod();
};
#endif
CPP
#include "TestClass.h"
TestClass::TestClass(PinName led, float flashRate) : m_Led(led, 0), m_FlashRate(flashRate)
{
//NOTE: The RTOS hasn't started yet, so we can't create the internal thread here
}
void TestClass::start()
{
m_rtosTimer = new RtosTimer(&TestClass::threadHelper, osTimerPeriodic, (void *)0);
int updateTime = (1.0 / m_FlashRate) * 1000;
m_rtosTimer->start(updateTime);
}
void TestClass::threadHelper(const void* arg)
{
//Cast the argument to a TestClass instance pointer
TestClass* instance = (TestClass*)arg;
//Call the thread method for the TestClass instance
instance ->threadMethod();
}
void TestClass::threadMethod()
{
//Do some stuff
}
谢谢
答案 0 :(得分:2)
传递给arg
的{{1}}指针是您传递给threadHelper()
构造函数的参数 - 在本例中是一个空指针。 RtosTimer
取消引用此空指针,导致运行时错误。
相反:
threadHelper()