Expand|Select|Wrap|Line Numbers
- class Buffer {
- static int dataBuffer[100];
- static int head = 0;
- static int tail = 0;
- public:
- static void Produce(int data) {
- dataBuffer[head++] = data;
- if (head == 100) head = 0;
- }
- static bool CanConsume() {
- if (head != tail) return true;
- return false;
- }
- static int Consume() {
- int pos = tail++;
- if (tail == 100) tail = 0;
- return dataBuffer[pos];
- }
- }
- void Producer() {
- while(1) {
- … some stuff …
- Buffer.Produce(x);
- … some stuff …
- }
- }
- void Consumer() {
- while(1) {
- … some stuff …
- if(Buffer.CanConsume() ) {
- int x = Buffer.Consume();
- … do something with x …
- }
- … some stuff …
- }
- }
b. If we have two threads running ‘Producer’ and one thread running ‘Consumer’ is there anything wrong with the code above? Give details.
c. If we have one thread Running ‘Producer’ and two threads running ‘Consumer’ is there anything wrong with the code above? Give details.