ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

I would suggest having 2 separate callbacks. And the code that requires certain variable to be set is executed only if respective flags are true. Also, I'd suggest using mutex on flags to avoid any race conditions. For eg.

class A 
{
    bool flag;
    std::mutex m;
    void callback1(...)
    {
         //Set the flag
         m.lock();
         flag = true;
         m.unlock();
         .
         .
         .
    }
    void callback2(...)
    {
        //Use flag to check if you wanna execute the code
       m.lock();
       bool isFlagTrue = flag;
       m.unlock();
       if (isFlagTrue)
       {
             //Put your code here ...
       }
};

I hope that's what you're looking for.

I would suggest having 2 separate callbacks. And the code that requires certain variable to be set is executed only if respective flags are true. Also, I'd suggest using mutex on flags to avoid any race conditions. For eg.

class A 
{
    bool flag;
    std::mutex m;
    void callback1(...)
    {
         //Set the flag
         m.lock();
         flag = true;
         m.unlock();
         .
         .
         .
    }
    void callback2(...)
    {
        //Use flag to check if you wanna execute the code
       m.lock();
       bool isFlagTrue = flag;
       m.unlock();
       if (isFlagTrue)
       {
             //Put your code here ...
       }
};

I hope that's what you're looking for.

EDIT:

As Kramer suggested in his comment below, only one callback happens at a time. So you don't have to use mutex unless you have multi-threaded node process.