简体   繁体   中英

Get elapsed time of std::future<T>::wait_for

Is there an easy way to get the time elapsed during std::future<T>::wait_for if no time-out occurred? I want to achieves something like this:

std::future<void> futureRet = std::async(std::launch::async, &Someone::doSomething, this);
futureRet.wait_for(std::chrono::seconds(30));

cout << "doSomething returned after <" << futureRet.getElapsedTime() << "> seconds.";

Is there a kind of "getElapsedTime()" function or do I have to calculate the elapsed time myself?

There is an easy way using <chrono> :

auto start = std::chrono::steady_clock::now();
std::future<void> futureRet = std::async(std::launch::async, &Someone::doSomething, this);
futureRet.wait_for(std::chrono::seconds(30));
auto end = std::chrono::steady_clock::now();
std::chrono::duration<double> elapsed_seconds = end - start;    
cout << "doSomething returned after <" << elapsed_seconds.count() << "> seconds.";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM