Hacker News new | past | comments | ask | show | jobs | submit login

Sometimes the verbosity annoys me a little while writing it, but I do think Rust made the right decision making you write code like the following to use the standard sleep function.

  std::thread::sleep(Duration::from_millis(300));



In Ada you can do something similar:

    delay 0.3;
Or with package Ada.Real_Time:

    delay To_Duration (Milliseconds (300));
Or use `delay until`:

    delay until Clock + Milliseconds (300);
`delay until` is useful in loops because `delay` sleeps at least the given duration, so you get drift. (You call Clock only once and store it in a variable before the loop and then update it adding the time span each iteration)


In fact std::thread::sleep doesn't exist. There's sleep_for and sleep_until. Time deltas and time points are incompatible at the type level, so you have to do:

   std::this_thread::sleep_until(std::chrono::system_clock::now() + 300ms)
That's true for all functions that take timeouts (At least since C++11).

edit: s/thread/this_thread/


GP's snippet was in Rust (not that I know if it's valid Rust code).

However, in C++ std::thread is a class and it does not have a static member function sleep_until. (However, std::this_thread, which is a namespace, does have such a function).


D'oh! Thanks for the correction! Rust using std:: for its standard library does make some code snippets ambiguous!


Yeah, I agree. :-)




Join us for AI Startup School this June 16-17 in San Francisco!

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: