How to sleep forever only using C++11 -
yes, use sleep() in windows or pause() in posix, , goes on. how sleep using c++11? thought there way, joining calling thread using std::this_thread std::this_thread has no join() method unlike pthread functions. not mention can't handle signals c++11 , know how iterate sleep forever below:
while(true) std::this_thread::sleep_for(std::chrono::seconds(1));
however, can see, it's not elegant @ all. code still consumes cpu time. scheduler has care process. use conditional variable or promise, again takes bit of memory or wouldn't work on os(it throw exception avoid deadlock).
maybe equivalent of sleep(infinite) of windows:
while(true) std::this_thread::sleep_for(std::chrono::hours::max());
but many it's not practical.
could think of brilliant way?
there 2 ways can think of.
one, @guiroux mentions sleep_until
non-reachable time:
std::this_thread::sleep_until(std::chrono::system_clock::now() + std::chrono::hours(std::numeric_limits<int>::max()));
or have wait indefinitely condition never fulfilled.
std::condition_variable cv; std::mutex m; std::unique_lock<std::mutex> lock(m); cv.wait(lock, []{return false;});
however can't see reason this.
Comments
Post a Comment