Передача функции с параметрами в пул потоков
имеется многопоточный producer consumer. Он должен вызывать функцию ProcessRequest с параметром полученным из GetRequest. К сожалению никак не получается сделать так, чтобы в пул потоков передавалась не только функция, но и параметры с которыми ее вызвать (Хочу делать это из main'а в цикле). Подскажите, как решить данную проблему. Спасибо! Вот мой producer consumer.
#include <iostream>
#include <thread>
#include <functional>
#include <chrono>
#include <queue>
#include <mutex>
#include <condition_variable>
#include <future>
#include <vector>
using namespace std;
class Request {
public:
Request() : a(0) {}
private:
int a;
};
class Stopper {
public:
Stopper() : stopSignal(false) {}
void setStopper(bool new_stopper) {
stopSignal = new_stopper;
}
bool getStopper() {
return stopSignal;
}
private:
bool stopSignal;
};
class ThreadPool {
public:
using Task = function<void()>;
explicit ThreadPool(size_t amThreads) {
start(amThreads);
}
~ThreadPool() {
stop();
}
void enqueue(Task task) {
{
unique_lock<mutex> lock{ eventmutex };
Tasks.emplace(move(task));
}
event.notify_one();
}
private:
vector<thread> Pool;
condition_variable event;
mutex eventmutex;
queue<Task> Tasks;
Stopper stopSignal;
void start(size_t amThreads) {
for (auto i = 0u; i < amThreads; ++i) {
Pool.emplace_back([=] {
while (true) {
Task task;
{
unique_lock<mutex> lock{ eventmutex };
event.wait(lock, [=] { return stopSignal.getStopper() || !Tasks.empty(); });
if (stopSignal.getStopper()) {
break;
}
task = move(Tasks.front());
Tasks.pop();
}
task();
}
});
}
}
void stop() noexcept {
{
unique_lock<mutex> lock{ eventmutex };
stopSignal.setStopper(true);;
}
event.notify_all();
for (auto& thread : Pool) {
thread.join();
}
}
};
void ProcessRequest(Request* request, Stopper stopSignal) {
}
Request* GetRequest() {
return 0;
}
int main() {
ThreadPool pool{ 10 };
Request request;
Stopper stopSignal;
return 0;
}