1

I'm trying to use an RtosTimer within a class but the mbed locks up. I think this is because I'm calling threadHelper each tick and its creating a new pointer whereas I actually want to call threadMethod each tick or call threadHeper each tick but use the same pointer.

Can anyone show me how I should be doing this?

The code below works for an RtosThread because threadHelper is only called once, but I need to use an 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
}

Thanks

4

1 回答 1

2

传递给的arg指针threadHelper()是你传递给RtosTimer构造函数的参数——在本例中是一个空指针。 threadHelper()取消引用这个导致运行时错误的空指针。

反而:

m_rtosTimer = new RtosTimer(&TestClass::threadHelper, osTimerPeriodic, (void*)this);
于 2015-03-13T22:58:24.773 回答