Ask Your Question

Correct way to interrupt a callback

asked 2020-04-05 15:42:40 -0600

anonymous user


updated 2020-04-05 15:43:55 -0600

I will present my problem with an example. I have a callback of Imu messages which I want to integrate on the fly. In a parallel thread I want to access the integrated value from the callback and reset the integration if some condition is met.

Let me present an example.

void Camera::imuCb(const sensor_msgs::Imu::ConstPtr& msg)
  if(start_integration_) {
      // integrate IMU
  } else if(reset_integration_) {
    // update integrated values in the following variables
    Rotation_; // this is Eigen::Matrix3d type and is a member of `camera` class which will be accessed in `camera::foo`
    updated_prior_ = true; // set this flag so that we know Rotation matrix is updated in camera::foo
    start_integration_ = true; // for next callback in this function
    // reset integration and start a fresh


void camera::foo(cv::Mat img)
    reset_integration_ = true;
    start_integration_ = false;
      // wait until Rotation matrix is updated in the imuCb function
      if(prior_updated_) {
        prior_updated_ = false; // ok, now it is updated, exit and process the variable
   // do some processing using hte updated matrix `Rotation_`

Note that camera::foo is running in the main thread.

The gist is thus:

Whenever camera::foo is called I want the integration to stop in the camera::imuCb and use the integrated Rotation_ in the camera::foo function.

My doubt is; is what I am doing the right approach? What is the most efficient way to implement this?

edit retag flag offensive close merge delete


Yes, that's something I am trying with right now. Although I don't think memory race would be an issue here since the next time I am going to update Rotation_ is in the next call to camera::foo.

anonymous userAnonymous ( 2020-04-05 17:00:28 -0600 )edit

I just wanted to be sure if what I am implementing is in fact the correct approach or if there were other more efficient approaches.

anonymous userAnonymous ( 2020-04-05 17:01:13 -0600 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2020-04-05 16:58:23 -0600

updated 2020-04-05 16:58:46 -0600

I didn't have time to make a better answer, but could you use a conditional (that is set True by foo) before assigning a value to the final variable. The callback wouldn't stop, however, the Rotation_ isn't going be updated and you are able to use the last value. Don't forget the memory race. - I'm not sure with I got the idea.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2020-04-05 15:42:40 -0600

Seen: 106 times

Last updated: Apr 05 '20