A weird game.
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.
turings-tarpit/scratchpad/threadtest.cpp

38 lines
874 B

#include <chrono>
#include <future>
#include <iostream>
#include <thread>
using namespace std::chrono_literals;
int main()
{
std::future<int> future = std::async(std::launch::async, []()
{
std::this_thread::sleep_for(3s);
return 8;
});
std::cout << "waiting...\n";
std::future_status status;
do
{
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;
}
}
while (status != std::future_status::ready);
std::cout << "result is " << future.get() << '\n';
}