How to sleep a C++ Boost Thread

Depending on your version of Boost: Either… #include <boost/chrono.hpp> #include <boost/thread/thread.hpp> boost::this_thread::sleep_for(boost::chrono::milliseconds(100)); Or… #include <boost/date_time/posix_time/posix_time.hpp> #include <boost/thread/thread.hpp> boost::this_thread::sleep(boost::posix_time::milliseconds(100)); You can also use microseconds, seconds, minutes, hours and maybe some others, I’m not sure.

How can I put the current thread to sleep?

Rust 1.4+ Duration and sleep have returned and are stable! use std::{thread, time::Duration}; fn main() { thread::sleep(Duration::from_millis(4000)); } You could also use Duration::from_secs(4), which might be more obvious in this case. The solution below for 1.0 will continue to work if you prefer it, due to the nature of semantic versioning. Rust 1.0+ Duration wasn’t …

Read more

How to “sleep” until timeout or cancellation is requested

I just blogged about it here: CancellationToken and Thread.Sleep in Short: var cancelled = token.WaitHandle.WaitOne(TimeSpan.FromSeconds(5)); In your context: void MyFunc (CancellationToken ct) { //… // simulate some long lasting operation that should be cancelable var cancelled = ct.WaitHandle.WaitOne(TimeSpan.FromSeconds(10)); }

Prevent windows from going into sleep when my program is running?

After considering vim’s answer “Using PowerCreateRequest, PowerSetRequest, and PowerClearRequest functions is the preferred method.” with the linked AvailabilityRequests.docx on msdn which is exhausting to get into it (too much to read), I have searched the web for a concrete example in c# that is based on the PowerCreateRequest and found http://go4answers.webhost4life.com/Example/problem-monitor-wakeup-service-windows7-12092.aspx [EDIT 2016 – isn’t …

Read more

What is _GLIBCXX_USE_NANOSLEEP all about?

When libstdc++ is built its configure script tests your system to see what features are supported, and based on the results it defines (or undefines) various macros in c++config.h In your case configure determined that the POSIX nanosleep() function is not available and the macro is not defined. However, as you say, nanosleep() is available …

Read more