61 lines
1.2 KiB
C++
61 lines
1.2 KiB
C++
// ThreadPool.hh
|
|
#include "shared_queue.hh"
|
|
|
|
#include <thread>
|
|
#include <vector>
|
|
|
|
#ifndef THREAD_POOL_H
|
|
#define THREAD_POOL_H
|
|
|
|
// Implementations for producers and consumers
|
|
void producer( const int id, SharedQueue<int>& _q, int max_t, int max_n );
|
|
void consumer( const int id, SharedQueue<int>& _q, int max_t, int max_n );
|
|
|
|
class ThreadPool
|
|
{
|
|
public:
|
|
ThreadPool() :
|
|
_q(),
|
|
_consumers(),
|
|
_producers()
|
|
{};
|
|
void newProducer( 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 newConsumer( 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<std::thread * > _consumers;
|
|
std::vector<std::thread * > _producers;
|
|
|
|
// queue
|
|
SharedQueue<int> _q;
|
|
};
|
|
#endif
|