Ask Your Question
0

timing function for while loop

asked 2012-08-28 03:45:27 -0600

Arash gravatar image

updated 2014-04-20 14:09:36 -0600

ngrennan gravatar image

I was just wondering, if I want to write a while loop which says while x seconds hasn't reach execute this statement, how would I go about it? I tried using while(ros::Duration(x).sleep()){ .... } but it doesn't work. also, tried creating a counter but there is no way to measure the count in seconds

thanks,

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
8

answered 2012-08-28 03:58:08 -0600

Lorenz gravatar image

updated 2012-08-28 05:15:05 -0600

Example code that should do what you want:

ros::Time start_time = ros::Time::now();
ros::Duration timeout(2.0); // Timeout of 2 seconds
while(ros::Time::now() - start_time < timeout) {
  // do your stuff
}
edit flag offensive delete link more

Comments

it's not working for me if I give 2 for a timeout the loops do not stop for milliseconds. I tried giving 100 and the same result. but when I gave 200 it remains for some time. but I cannot find the relationship

akash12124234 gravatar image akash12124234  ( 2021-11-06 09:10:18 -0600 )edit

Your Answer

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

Add Answer

Question Tools

Stats

Asked: 2012-08-28 03:45:27 -0600

Seen: 2,767 times

Last updated: Aug 28 '12