0
$\begingroup$

I don't really understand the concept of mutex. Suppose I have two callbacks running paralllel(for topic1 and topic2).Both of their topic pub rate is same so I am getting the data at the same rate in both of them. The topic1 gets me length and topic2 breath and I publish this whole to topic3 as area.

I want to take length first and then multiply it with breath.

Now can I put length into a variable and lock in breath callback till I get the updated length value. otherway to consider suppose my topic1 for length has not started yet,can I hold area to update in that case. Also if there are some good resources to learn about thread,can you please share.

Thanks,love robotics.

$\endgroup$

1 Answer 1

0
$\begingroup$

There could be many ways in which you can approach this. Possibly better then the one I am about to suggest as it depends on the actual application. you can try something like this

  • variable length_ which you update in your length cb
  • variable width_ which you update in your width cb
  • You create a timer callback in you node which will run at the same rate as your publishers rate.
  • Inside this callback you copy the length value and reset the length_ variable to something not possible like -1 and then do the same for breadth_.
  • Now every time the timer callback hits you can check if the length_ value is -1, if not copy and reset the value, copy and reset breadth value, calculate area and publish.
  • If you don't create any callback groups, you should not need any mutex as all three callbacks will run mutually exclusively.
$\endgroup$
4
  • $\begingroup$ Hi, Thanks for the reply.I want to use mutex to wait and hold as the number of parameters here is one to wait/hold on.This becomes a little complex with more feature addition. If I have a callback function and I want it to be on hold cause I want to perform certain set of algos on it. Does mutex helps in this case? $\endgroup$ Mar 4 at 7:06
  • $\begingroup$ yes you can do that with a mutex, but then you'll have to make sure both of your callbacks can execute in parallel i.e. either the callback group they belong to is reenterent or they belong to independent callback groups otherwise blocking one callback will result in blocking of the other callback as well in which case you'll keep waiting for the other value to update which will never happen. $\endgroup$
    – tomRob
    Mar 4 at 7:23
  • $\begingroup$ I see mutex block all event,right?All callback and timers,is it? $\endgroup$ Mar 4 at 11:05
  • $\begingroup$ Not event but threads, any number of parallel threads will get blocked if they are using the same mutex and only one of them will have access to it at a time. $\endgroup$
    – tomRob
    Mar 5 at 1:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.