Skip to content

task_builder

Alexy Pellegrini edited this page May 11, 2021 · 4 revisions

nes::task_builder

Defined in header <nes/thread_pool.hpp>

class task_builder;

Description

nes::task_builder is the helper class to generate nes::task_list. Once built a task list is immutable, but reusable.

Public Member functions

Function Description
task_builder Creates a new task builder
~task_builder Destroys the task builder
operator= Assigns a task builder
execute Pushes a single task in the task list, without returning a task result
invoke Pushes a single task in the task list
dispatch Pushes a task in the task list that will be called multiple times
barrier Inserts an execution dependency between all previous tasks and any further tasks
checkpoint Inserts an checkpoint that will be signaled once all previous tasks are done
fence Inserts an execution dependency between all previous tasks and any other part of the program
build Build the nes::task_list and clear the builder

Example

main.cpp

#include <iostream>

#include <nes/thread_pool.hpp>

int main()
{
    nes::task_builder builder{};

    builder.dispatch(42, 12, 3, [](std::uint32_t x, std::uint32_t y, std::uint32_t z)
    {
        //Do something
    });

    nes::task_checkpoint checkpoint{builder.checkpoint()}; // Will be signaled once the dispatch is done
    nes::task_fence      fence     {builder.fence()};      // Will blocks before the execute until we signal it

    builder.execute([]()
    {
        //Do something
    });
    
    nes::task_list list{builder.build()};

    nes::thread_pool thread_pool{};

    std::cout << "Launching first the work" << std::endl;

    std::future<nes::task_list> future{thread_pool.push(list)};
    std::cout << "Work started" << std::endl;

    checkpoint.wait();
    std::cout << "First task done" << std::endl;

    fence.signal();
    std::cout << "Second task started" << std::endl;

    future.wait(); //Waits for the whole task list to end
    std::cout << "Second task done" << std::endl;

    list = future.get(); //We can get back our task list and reuse it
}

Output

Launching first the work
Work started
Second task started
Second task done
Clone this wiki locally