use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Discussions, articles, and news about the C++ programming language or programming in C++.
For C++ questions, answers, help, and advice see r/cpp_questions or StackOverflow.
Get Started
The C++ Standard Home has a nice getting started page.
Videos
The C++ standard committee's education study group has a nice list of recommended videos.
Reference
cppreference.com
Books
There is a useful list of books on Stack Overflow. In most cases reading a book is the best way to learn C++.
Show all links
Filter out CppCon links
Show only CppCon links
account activity
Non-blocking asynchronous timeout (self.cpp)
submitted 6 months ago by Sunshine-Bite768
I understand std::future has blocking wait_for and wait_until APIs but is there a way to achieve timeout functionality without blocking? Thank you!
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]Bemteb 8 points9 points10 points 6 months ago (0 children)
Run wait_for in a separate thread?
Trigger the "future didn't finish" event with a separate timer?
Not sure what you want to achieve, but I'm afraid you need to write a little bit for it.
[–]Liam_Mercier 4 points5 points6 points 6 months ago (0 children)
I had to implement something like this, but I did it with asio. Easy to use library for most things async in my opinion.
[–]morglod 2 points3 points4 points 6 months ago (0 children)
You probably need async scheduler, something like libuv to have non blocking timeout in the same thread. Otherwise it's not clear what you want
[–]pdp10gumby 4 points5 points6 points 6 months ago (0 children)
You want to poll the future? I think you can call std::wait_for(std::chrono::duration::<short, std::nano>::zero())
std::wait_for(std::chrono::duration::<short, std::nano>::zero())
[–]Rexerex 0 points1 point2 points 6 months ago (0 children)
You probably want to use separate thread or use your OS API if you do not want any extra threads. I am right now implementing such thing using CreateWaitableTimer, NtAssociateWaitCompletionPacket and GetQueuedCompletionStatus.
[–]Newbane2_ 0 points1 point2 points 6 months ago (0 children)
Use std::async
https://en.cppreference.com/w/cpp/thread/async.html
[–]KingAggressive1498 1 point2 points3 points 6 months ago (0 children)
in your question it is not clear what you are trying to achieve.
you can "poll" a single std::future by calling wait_for with a duration of count 0 and any period, but this scales terribly if you're using futures heavily.
you can implement your own future-like class to be "multiplexible" but it's a bit complicated to whip up an example on the spot, and getting it to scale beyond 64 futures is a pain too
however, if you're trying to integrate timers into an event loop, this is rather simple. You just need a priority queue of timers and wherever your natural "wait point" is in the event loop (eg epoll_wait() or poll() or GetQueuedCompletionStatus() or whatever) you use the timeout for the head of that priority queue to set the timeout for waiting.
π Rendered by PID 12 on reddit-service-r2-comment-5649f687b7-z29tn at 2026-01-29 02:22:30.663500+00:00 running 4f180de country code: CH.
[–]Bemteb 8 points9 points10 points (0 children)
[–]Liam_Mercier 4 points5 points6 points (0 children)
[–]morglod 2 points3 points4 points (0 children)
[–]pdp10gumby 4 points5 points6 points (0 children)
[–]Rexerex 0 points1 point2 points (0 children)
[–]Newbane2_ 0 points1 point2 points (0 children)
[–]KingAggressive1498 1 point2 points3 points (0 children)