Get the ThreadID of a HighResolutionTimer thread or how to figure out if a call came from the HighResolutionTimer callback?


#1

The title says it all, I have a class that runs a private HighResolutionTimer to perform callbacks to a virtual method of this class on a regular basis. Now I need to find out if some special member function of my class was called from within the timer callback function invoked or from somewhere else. As far as I can see there is no possibility to retrieve the ThreadID of the HighResolutionTimer thread, am I right?


#2

I would simplify that, just so by contract nobody else is calling the timerCallback(). So you always know.
If you have code, that can occur either by timerCallback or by something else, create a private method with a flag:

class Foo : private HighResolutionTimer
{
// ...
private:
    void doSomething (bool fromTimer=false);
    void hiResTimerCallback() override { doSomething (true); }
};

Would that work for you?


#3

It’s not that easy. My situation looks a bit like this:

class Foo : private HighResolutionTimer
{
public:
    virtual void timerCallback (Bar& something) = 0;

    // needs to execute slightly different tasks if called from within Foo::timerCallback or from somewhere outside
    void stopCallbacks();

private:
    void hiResTimerCallback() override
    {
        Bar bar = createBar();
        timerCallback (bar);
        doSomethingWithBar (bar);
    }
}

#4

Well, seems to work pretty well with that use case:

void doSomethingWithBar (Bar& bar, bool fromTimer = false);

private:
    void hiResTimerCallback() override
    {
        Bar bar = createBar();
        timerCallback (bar);
        doSomethingWithBar (bar, true);
    }

It is still much easier and more reliable than storing the ThreadID of the timer and checking each time…


#5

Well, ok, I agree that it would work that way, however all this belongs to a more generic set of classes and subclasses, where there are cases that a timer is not even invoked. For the sake of an easy-to-use interface I’d really like this check to happen automatically. I just didn’t want to over-complicate my initial posts so I tried to keep the examples as simple as possible.

Now the real situation looks more like this example and I see no real better solution than retrieving the current ThreadID of the timer at the start of each callback (or maybe do it once and then set a flag that it was set, not sure how expensive the task of getting the ThreadID is)

class FooBase
{
public:
    //...
    virtual void callback (Bar& something) = 0;

    virtual void stopCallbacks() = 0;
}

class FooWithNonTimerBackend
{
public:
     void stopCallbacks() override; // No timer involved in this case
     //...
}

class FooWithTimerBackend : private HighResolutionTimer
{
public:
    // In this particular subclass it would be important to know where the call came from 
    void stopCallbacks() override
    {
        if (Thread::getCurrentThreadID == hisResTimerThreadID)
            // ... do something special
        
       //... do the other stuff
    }
    //...

private:
    Thread::ThreadID hisResTimerThreadID = nullptr;

    void hiResTimerCallback() override
    {
        hisResTimerThreadID = Thread::getCurrentThreadId();

        Bar bar = createBar();
        timerCallback (bar);
        doSomethingWithBar (bar);
    }
}