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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
|
#pragma once
#include "IThreadPool.h"
#include "ThreadSync.h"
#include "Thread.h"
#include <list>
#include <Containers/SmallVector.h>
using namespace Death::Containers;
namespace nCine
{
/// Thread pool
class ThreadPool : public IThreadPool
{
public:
/// Creates a thread pool with as many threads as available processors
ThreadPool();
/// Creates a thread pool with a specified number of threads
explicit ThreadPool(std::size_t numThreads);
~ThreadPool() override;
/// Enqueues a command request for a worker thread
void EnqueueCommand(std::unique_ptr<IThreadCommand>&& threadCommand) override;
private:
#ifndef DOXYGEN_GENERATING_OUTPUT
// Doxygen 1.12.0 outputs also private structs/unions even if it shouldn't
struct ThreadStruct
{
std::list<std::unique_ptr<IThreadCommand>>* queue;
Mutex* queueMutex;
CondVariable* queueCV;
bool shouldQuit;
};
#endif
std::list<std::unique_ptr<IThreadCommand>> queue_;
SmallVector<Thread, 0> threads_;
Mutex queueMutex_;
CondVariable queueCV_;
Mutex quitMutex_;
std::size_t numThreads_;
ThreadStruct threadStruct_;
static void WorkerFunction(void* arg);
/// Deleted copy constructor
ThreadPool(const ThreadPool&) = delete;
/// Deleted assignment operator
ThreadPool& operator=(const ThreadPool&) = delete;
};
}
|