You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
38 lines
874 B
38 lines
874 B
2 months ago
|
#include <chrono>
|
||
|
#include <future>
|
||
3 months ago
|
#include <iostream>
|
||
2 months ago
|
#include <thread>
|
||
2 months ago
|
using namespace std::chrono_literals;
|
||
3 months ago
|
|
||
|
int main()
|
||
|
{
|
||
2 months ago
|
std::future<int> future = std::async(std::launch::async, []()
|
||
2 months ago
|
{
|
||
2 months ago
|
std::this_thread::sleep_for(3s);
|
||
|
return 8;
|
||
|
});
|
||
|
|
||
|
std::cout << "waiting...\n";
|
||
|
std::future_status status;
|
||
2 months ago
|
|
||
2 months ago
|
do
|
||
2 months ago
|
{
|
||
2 months ago
|
status = future.wait_for(100ms);
|
||
|
switch (status)
|
||
|
{
|
||
|
case std::future_status::deferred:
|
||
|
std::cout << "deferred\n";
|
||
|
break;
|
||
|
case std::future_status::timeout:
|
||
|
std::cout << "timeout\n";
|
||
|
break;
|
||
|
case std::future_status::ready:
|
||
|
std::cout << "ready!\n";
|
||
|
break;
|
||
|
}
|
||
2 months ago
|
}
|
||
2 months ago
|
while (status != std::future_status::ready);
|
||
3 months ago
|
|
||
2 months ago
|
std::cout << "result is " << future.get() << '\n';
|
||
3 months ago
|
}
|