26 lines
911 B
C++
26 lines
911 B
C++
|
#include "producer.h"
|
||
|
|
||
|
//---------------------------------------
|
||
|
template<typename T> QSemaphore Producer<T>::numProducts;
|
||
|
template<typename T> Monitor<T> * Producer<T>::mon;
|
||
|
template<> int (*Producer<int>::produce)(int) = NULL;
|
||
|
//---------------------------------------
|
||
|
|
||
|
template<typename T> void Producer<T>::initClass(int numP, Monitor<T> *m, T(*prod)(int)) {
|
||
|
mon = m;
|
||
|
numProducts.release(numP);
|
||
|
produce = prod;
|
||
|
}
|
||
|
//---------------------------------------
|
||
|
|
||
|
template<typename T>
|
||
|
void Producer<T>::run() {
|
||
|
while (numProducts.tryAcquire()) { // While not all numProducts items are produced:
|
||
|
T item = (*produce)(ID); // Produce one item
|
||
|
T* aux = mon->canPut(); // Get place for item in emptySpotsQ
|
||
|
*aux = item; // Put item into emptySpotsQ
|
||
|
mon->donePutting(aux); // Give info to consumer threads
|
||
|
}
|
||
|
}
|
||
|
//---------------------------------------
|