Search code examples
c++mutexpublish-subscriberosrace-condition

Avoiding data race condition between two ROS subscriber callback funtions


Let's suppose we've two ROS subscriber callback functions where the callback is called every time a new message is extracted from the queue and we want to use the value of a callback in another callback and vice versa.

I've implemented this in a class with two member variables that store my data.

I suspect a possible race condition between the two callbacks. I tried to create a simple example below.

class <class_name>
{
public:
    <var_1_type> get_var_1() {
        return var_1;
    }
    set_var_1(const <var_1_type> value) {
        var_1 = value;
    }
    <var_2_type> get_var_2() {
        return var_2;
    }
    set_var_2(const <var_2_type> value) {
        var_2 = value;
    }

private:
    <var_1_type> var_1;
    <var_2_type> var_2;
    void callback_function_1(<msg_type> &msg_holder);
    void callback_function_2(<msg_type> &msg_holder);
};

void <class_name>::callback_function_1(<msg_type> &msg_holder)
{
    set_var_1(msg_holder.data);
    // Use var_1 and var_2 to create a new data, e.g.,
    <var_3_type> var_3 = get_var_1() * get_var_2();
    // Now we can publish the var_3, which is the output of the node.
    var_3_pub.publish(var_3);
}

void <class_name>::callback_function_2(<msg_type> &msg_holder)
{
    // Use the var_1 and msg_holder.data to calculate var_2, e.g.,
    <var_2_type> var_2_ = get_var_1() + msg_holder.data;
    set_var_2(var_2_);
}

int main(int argc, char** argv)
{
    // Instantiate an object of type <class_name>
    // Go into ros spin and let the callback functions do all the work.
    return 0;
}

In my particular application, var_1 and var_2 are in fact, 2D vectors that can be thought as a matrix, I don't want the contents of the matrix to be modified by one callback function while it's being used by the other callback function.

I'm somewhat familiar with the use of std::lock_guard<std::mutex> guard(mu);, mu.lock();, mu.unlock(). However, I cannot immediately see a way of using <mutex> in this case. Any help is appreciated.


Solution

  • You simply can make use of a Lock Guard using the same mutex instance in each of your callbacks like:

    std::mutex mutex_;
    
    void <class_name>::callback_function_1(<msg_type> &msg_holder)
    {
        const std::lock_guard<std::mutex> lock(mutex_);
        //Locked code here
        //The mutex is automatically released when lock goes out of scope (function left)
    }
    
    void <class_name>::callback_function_2(<msg_type> &msg_holder)
    {
        const std::lock_guard<std::mutex> lock(mutex_);
        //Locked code here
        //The mutex is automatically released when lock goes out of scope (function left)
    }
    

    The complete code in your callbacks is executed threadsafe with this solution. Note that you also are able to lock more specific parts of your function by creating a thighter scope { ... }.