The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

Madhusudhan Srikkanth (7) [Avatar] Offline
#1
Hi Anthony,

You should make spawn_async function introduced in Page 111 (Listing 4.17) a variadic template to accept any number of arguments.

This is so because the code in Listing 4.23 and others assumes this is the case.

Here is a straightforward implementation:

template<typename Func, typename... Args>
auto spawn_async(Func&& func, Args&&... args)
{
    std::experimental::promise<std::result_of_t<Func(Args&&...)>> prom;
    auto res = prom.get_future();
    auto lambda = [p = std::move(prom), f=std::decay_t<Func>(func)](auto&&... a) mutable
                    {
                            try
                            {
                                    p.set_value_at_thread_exit(f(std::forward<decltype(a)>(a)...));
                            }
                            catch (...) 
                            {
                                    p.set_exception_at_thread_exit(std::current_exception());
                            }
                    };              
    std::thread t(std::move(lambda), std::forward<Args>(args)...);
    t.detach();
    return res;                     
}


Thanks
Madhusudhan Srikkanth