Код: Выделить всё
#include
#include
#include
#include
#include
#include
#include
class Job{
};
std::queue jobs;
std::unique_ptr mutex = std::make_unique();
std::vector threads;
std::vector threadsEvents;
std::stack freeThreads;
std::atomic_bool stopRequested { false };
void DoJob(Job job = {})
{
}
void ThreadedMethod(const std::size_t threadNumber)
{
while (!stopRequested) {
mutex->lock();
if (!jobs.empty()) {
//auto job = queue.front();
//queue.pop();
mutex->unlock();
DoJob(/*std::move(job)*/);
} else {
freeThreads.push(threadNumber);
mutex->unlock(); // 1)
// Unlocking before thread suspended.
::SetEvent(threadsEvents[threadNumber]); // Notify that thread will be suspended
::SuspendThread(threads[threadNumber]); // 2)
}
}
}
void addJob(Job job){
std::lock_guard lock {*mutex};
jobs.push(job);
// activate first available free thread
// ResumeThread(freeThreads.top());
// ...
}
/*
// Creating threads
//for (std::size_t i = 0; i < 10; ++i) {
// Concidering the 'threadNumber' is stored somewhere.
// threads[i] = CreateThread(NULL, 0, ThreadedMethod, (LPVOID)&threadNumber, CREATE_SUSPENDED, NULL);
//}
*/
Подробнее здесь: https://stackoverflow.com/questions/794 ... his-thread