c++ - Can I execute on get my `std::future` and wait on it too? -


so can create std::future no work until .get() called:

auto f_deferred = std::async( std::launch::deferred, []{ std::cout << "i ran\n"; } ); 

you can write std::future waitable, , can made ready @ point code in thread:

std::packaged_task<void()> p( []( std::cout << "i ran\n"; } ); auto f_waitable = p.get_future(); 

if call f_deferred.wait_for(1ms), won't bother waiting. if call f_deferred.get(), lambda of choice (in case, 1 prints "i ran\n" executes.

if call f_waitable.get(), there no way code managing tasks aware waiting on future. if call f_deferred.wait(1ms);, future_status::deferred immediately.

is there way can combine these two?

a concrete use case thread pool returning futures when people queue tasks. if unqueued future .get()'d, want use thread blocked execute task rather having idle. on other hand, want people returned futures able determine if task finished, , wait bounded amount of time task finished. (in case waiting, i'm ok thread being idle during wait)

failing that, there solutions in upcoming proposals solve problem better having thread pool return future of limitations? i've heard there no future in futures , better solutions exist problem futures solve.


Comments

Popular posts from this blog

neo4j - finding mutual friends in a cypher statement starting with three or more persons -

php - How to remove letter in front of the word laravel -

minify - Minimizing css files -