Skip to content

thread_pool

Alairion edited this page May 8, 2021 · 4 revisions

nes::thread_pool

Defined in header <nes/thread_pool.hpp>

class thread_pool;

Description

nes::thread_pool

Public Member functions

Function Description
thread_pool Creates a new thread pool
~thread_pool Destroys the thread pool
operator= Not assignable
execute Pushes a single task in the thread pool, without returning a future
invoke Pushes a single task in the thread pool
push Pushes a task list in the thread pool
wait_idle Blocks the calling thread until all tasks submitted to the thread pool are done
thread_count Returns the number of threads of the thread pool

Example

main.cpp

#include <iostream>
#include <mutex>

#include <nes/thread_pool.hpp>

int main()
{
    std::mutex cout_mutex{};
    nes::thread_pool thread_pool{};

    thread_pool.execute([&cout_mutex](std::int32_t i)
    {
        std::lock_guard lock{cout_mutex};
        std::cout << "Execute: " << i << std::endl;
    }, 12);

    std::future<std::int32_t> future = thread_pool.invoke([&cout_mutex](std::int32_t i)
    {
        std::lock_guard lock{cout_mutex};
        std::cout << "Invoke: " << i << std::endl;
        return i * 2;
    }, 42);

    std::lock_guard lock{cout_mutex};
    std::cout << "Invoke result: " << future.get() << std::endl;
}

Possible output

Execute: 12
Invoke: 42
Invoke result: 84
Clone this wiki locally