summaryrefslogtreecommitdiff
path: root/thirdparty/etcpak/TaskDispatch.hpp
blob: b513de4c0c5cbedc94dd4b0151d540afcae1e201 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#ifndef __DARKRL__TASKDISPATCH_HPP__
#define __DARKRL__TASKDISPATCH_HPP__

#include <atomic>
#include <condition_variable>
#include <functional>
#include <mutex>
#include <thread>
#include <vector>

class TaskDispatch
{
public:
    TaskDispatch( size_t workers );
    ~TaskDispatch();

    static void Queue( const std::function<void(void)>& f );
    static void Queue( std::function<void(void)>&& f );

    static void Sync();

private:
    void Worker();

    std::vector<std::function<void(void)>> m_queue;
    std::mutex m_queueLock;
    std::condition_variable m_cvWork, m_cvJobs;
    std::atomic<bool> m_exit;
    size_t m_jobs;

    std::vector<std::thread> m_workers;
};

#endif