// ThreadPool.hh #include "shared_queue.hh" #include #include #ifndef THREAD_POOL_H #define THREAD_POOL_H // Implementations for producers and consumers void producer( const int id, SharedQueue& _q, int max_t, int max_n ); void consumer( const int id, SharedQueue& _q, int max_t, int max_n ); class ThreadPool { public: ThreadPool() : _q(), _consumers(), _producers() {}; void addProducer( int max_t = 3000, int max_n = 10) { std::thread * tp = new std::thread( producer, _producers.size(), std::ref(_q), max_t, max_n ); _producers.push_back(tp); } void addConsumer( int max_t = 2000, int max_n = 10) { std::thread * tp = new std::thread( consumer, _consumers.size(), std::ref(_q), max_t, max_n ); _consumers.push_back(tp); } void run() { // join all producers for ( int i = 0; i < _producers.size() ; i++ ) { (*_producers[i]).join(); } // join all consumers for ( int i = 0; i < _consumers.size() ; i++ ) { (*_consumers[i]).join(); } } private: // Consumer and Producers threads std::vector _consumers; std::vector _producers; // queue SharedQueue _q; }; #if ENABLE_PYBIND /* * Define Python access */ #include namespace py = pybind11; PYBIND11_MODULE(ThreadPool, tp) { py::class_( tp, "ThreadPool") .def(py::init()) .def("addProducer", &ThreadPool::addProducer, "Add a new Producer to the Pool") .def("addConsumer", &ThreadPool::addConsumer, "Add a new Consumer to the Pool") .def("run", &ThreadPool::run, "Run all registered Producers and Consumers"); } #endif #endif