Ask Your Question
1

Differences between ros::Duration::sleep and std::this_thread::sleep_for?

asked 2019-03-13 10:02:07 -0500

ssnover gravatar image

My team is developing in modern C++ and this is my first major project using ROS, but not my first major project using modern C++. I generally prefer using standard library functions to third party library functions, and so I generally use std::this_thread::sleep_for in order to sleep the current thread context. This is different from much of the existing codebase which uses ROS's ros::Duration::sleep and similar methods. I've looked through the source a little bit, but can't seem to find the actual implementation of ROS's sleep method.

Is there a difference between the implementations of these two methods I should be aware of? If not, have many developers stopped using ros::Duration in favor of standard functions?

edit retag flag offensive close merge delete

Comments

2
VictorLamoine gravatar image VictorLamoine  ( 2019-03-13 10:37:52 -0500 )edit
1

One thing to keep in mind IIRC is that ROS's Time and Duration classes and associated methods take into account simulated time if you're running in a sim or using a bag file which standard library functions won't take into account

jayess gravatar image jayess  ( 2019-03-13 15:02:02 -0500 )edit

@jayess: I think your comment is the answer, and I suggest reposting it as an answer

knxa gravatar image knxa  ( 2019-03-14 02:36:10 -0500 )edit

@VictorLamoine: +1 for the code reference, but please use permalinks. Otherwise future readers could be directed to completely unrelated lines due to code refactoring, addition, etc.

I've updated the link to be a permalink.

gvdhoorn gravatar image gvdhoorn  ( 2019-03-17 15:36:03 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
2

answered 2019-03-14 03:05:58 -0500

jayess gravatar image

If I recall correctly, ROS's Time and Duration classes and associated methods take into account simulated time if you're running in a sim or using a bag file, This is something that the standard library functions won't take into account. I can't find anything official stating this, but I've seen it around this site that it's encouraged to use ros::Duration::sleep and similar methods in favor of the standard functions.

Now, maybe you don't care about simulated time or bag files right now, but if you end up caring about them later you may have to go back and do significant rewrites which is no fun.

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

2 followers

Stats

Asked: 2019-03-13 10:02:07 -0500

Seen: 613 times

Last updated: Mar 14 '19