How std :: packaged_task works

I am analyzing the following code fragment and trying to understand it in detail:

template<typename FUNCTION, typename... ARGUMENTS>
auto ThreadPool::add( FUNCTION&& Function, ARGUMENTS&&... Arguments ) -> std::future<typename std::result_of<FUNCTION(ARGUMENTS...)>::type>
{
    using PackedTask = std::packaged_task<typename std::result_of<FUNCTION(ARGUMENTS...)>::type()>;

    auto task = std::make_shared<PackedTask>(std::bind(std::forward<FUNCTION>(Function), std::forward<ARGUMENTS>(Arguments)...));

    // get the future to return later
    auto ret = task->get_future();

    {
        std::lock_guard<std::mutex> lock{jobsMutex};
        jobs.emplace([task]() { (*task)(); });
    }

    // let a waiting thread know there is an available job
    jobsAvailable.notify_one();

    return ret;
}

I have few questions regarding std::packaged_task.

As you can see in the body of the method add(...), an instance std::packaged_taskis taska local variable whose scale ends with the completion of the method. The return value of the rettype is std::futurereturned by the copy. The value retis issued from task(which is local). Thus, as soon as the execution of the method is completed, it taskgoes beyond the scope, and therefore I expect the connected connected instance of std :: future to become invalid, do I understand correctly?

, , std::queue<Job> jobs. operator() of std::packaged_task, Function, , std::queue? std::packaged_task, ...?

, ThreadPool, https://github.com/dabbertorres/ThreadPool , , . , , , , ... , - , ...

, . Cheers Martin

+4
3

add (...), std:: packaged_task - - , .

, std::shared_ptr<PackedTask>. , 1. 0, , . , jobs , - .

ret std:: future .

. , ret move, .

, std:: future , ?

, task . jobs , , , ret ( task). , , - .

std:: packaged_task, ...?

, std::packaged_task . , / . std::bind , std::packaged_task . , / packaged_task, . , .

, , task , .

+3

, , , , std:: future , ?

task - std::shared_ptr, , jobs - , .

+1

, , , , std:: future , ?

.

future - . .

Think of it as encapsulating a special kind shared_ptrin an object (opaque) that controls the state of the promise / future dependency.

0
source

Source: https://habr.com/ru/post/1687198/


All Articles