I already have a design for my Producer Consumer problem with restrictions.
struct data{
int id;
int value;
};
std::list<struct data> g_DataQ;
class Consumer{
void process_data(){
struct data my_data = g_DataQ.pop_front();
if(my_data.id == 0){
//Consumer 0
}else if(my_data.id == 1){
//Consumer 1
}
//Process Data
}
};
void consume(struct data pData){
g_DataQ.push_back(pData);
}
Description:
Producer is calling "consume" function. And there can be multiple producer/consumer. That data to be consumed by current Consumer is determined by data.id
. For every consumer we instantiate a class Consumer
. But there is only one consume function.
Restrictions that I have:
- "consume" function has to be global.
- There can be multiple Producer/Consumer
My main concern is having the global g_DataQ. One way to resolve this is having a list of list where first dimension handles which consumer it belongs to.
My query can it be done without making the Q global?
Thanks in advance.