11 #ifndef PARALLEL_CELLULAR_AUTOMATA_THREADPOOL_HPP
12 #define PARALLEL_CELLULAR_AUTOMATA_THREADPOOL_HPP
19 #include <type_traits>
28 std::vector<std::thread> &threads;
53 using QueuesContentType = std::function<void()>;
87 template <
class F,
class... Args>
88 auto submit(F &&f, Args &&...args) -> std::future<
typename std::result_of<F(Args...)>::type>
91 using return_value_type =
typename std::result_of<F(Args...)>::type;
96 auto task = std::make_shared<std::packaged_task<return_value_type()>>(
97 std::bind(std::forward<F>(f), std::forward<Args>(args)...));
99 std::future<return_value_type> future_result(task->get_future());
104 local_queue->
push([task]() {
111 threadpool_work_queue.
push([task]() { (*task)(); });
114 return future_result;
118 std::atomic<bool> done;
120 std::vector<std::unique_ptr<WorkStealingQueue<QueuesContentType>>> workers_queues;
121 std::vector<std::thread> threads;
125 bool try_stealing_work(QueuesContentType &result)
127 unsigned qsize = workers_queues.size();
128 for (
unsigned i = 0; i < qsize; ++i)
130 unsigned const index = (thread_index + 1) % qsize;
131 if (workers_queues[index]->try_steal(result))
141 static thread_local
unsigned thread_index;
144 void worker_thread(
unsigned index);
RAII class to join threads.
Definition: threadpool.hpp:26
ThreadJoiner(std::vector< std::thread > &t)
Construct a new ThreadJoiner object.
Definition: threadpool.cpp:17
~ThreadJoiner()
Destroy the Thread Joiner object and in doing this join all the threads in the vector.
Definition: threadpool.cpp:21
void push(T elem)
Insert an element into the queue.
Definition: queues.hpp:51
void push(T data)
Push data into the queue.
Definition: queues.hpp:179
Work-stealing threadpool.
Definition: threadpool.hpp:51
~Threadpool()
Destroy the Threadpool object.
Definition: threadpool.cpp:71
size_t get_number_workers() const
Get the number of worker threads.
Definition: threadpool.cpp:66
auto submit(F &&f, Args &&...args) -> std::future< typename std::result_of< F(Args...)>::type >
Submit work to the threadpool. This function takes a function and its arguments and returns a future ...
Definition: threadpool.hpp:88
Threadpool(unsigned nw=0)
Construct a new Threadpool object.
Definition: threadpool.cpp:40
Namespace of the framework.
Definition: barrier.hpp:20
This file contains the definition and implementation of some useful thread-safe queues.