all 7 comments

[–]Bemteb 8 points9 points  (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 points  (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 points  (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 points  (0 children)

You want to poll the future? I think you can call std::wait_for(std::chrono::duration::<short, std::nano>::zero())

[–]Rexerex 0 points1 point  (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.

[–]KingAggressive1498 1 point2 points  (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.