1
#include<bits/stdc++.h>
#include<pthread.h>
#include<unistd.h>
#define MAX 10
using namespace std;
class BoundedBuffer
{
private:
  int buffer[MAX];
  int fill, use;
  int fullEntries;
  pthread_mutex_t monitor;  // monitor lock
  pthread_cond_t empty;
  pthread_cond_t full;
public:
  BoundedBuffer ()
  {
    use = fill = fullEntries = 0;
  }
  void produce (int element)
  {
    pthread_mutex_lock (&monitor);
    while (fullEntries == MAX)
      pthread_cond_wait (&empty, &monitor);
    buffer[fill] = element;
    fill = (fill + 1) % MAX;
    fullEntries++;
    //sleep(rand()%2);
    pthread_cond_signal (&full);
    pthread_mutex_unlock (&monitor);
  }
  int consume ()
  {
    pthread_mutex_lock (&monitor);
    while (fullEntries == 0)
      pthread_cond_wait (&full, &monitor);
    int tmp = buffer[use];
    use = (use + 1) % MAX;
    fullEntries--;
    //sleep(rand()%2);
    pthread_cond_signal (&empty);
    pthread_mutex_unlock (&monitor);
    return tmp;
  }
}b;
void* producer(void *arg){
int i=1;
while(true){
b.produce(i);
i++;
}
}
void* consumer(void *arg){
while(true){
cout<<b.consume()<<" ";
}
}
int main(){
pthread_t t1,t2;
pthread_create(&t1,NULL,producer,NULL);
pthread_create(&t2,NULL,consumer,NULL);
pthread_join(t1,NULL);
pthread_join(t2,NULL);
return 0;
}

Whenever sleep() is added in BoundedBuffer.consume() and BoundedBuffer.produce(int ) then it doesn't print any output.But when there is no sleep() in these functions then it works fine and print output as it should be .Why it's happening ?
Reference: http://pages.cs.wisc.edu/~remzi/OSTEP/threads-monitors.pdf

sonus21
  • 5,178
  • 2
  • 23
  • 48
  • By the way, it's generally better to put the signal outside of the critical section. – kec Nov 24 '14 at 06:58
  • @kec - that's not true. See http://stackoverflow.com/questions/4544234/calling-pthread-cond-signal-without-locking-mutex for why – Sean Nov 24 '14 at 15:23
  • @Sean: The selected answer on that question is misleading. The initial case given has no lock at all, which is not what I was suggesting. As to which is better, I'd argue that in non real-time applications, outside the critical section is still better. See the second answer to that question and the referenced Google groups post for details. – kec Nov 24 '14 at 15:39
  • @Sean: See this also: http://stackoverflow.com/questions/6419117/signal-and-unlock-order/6419626#6419626 . – kec Nov 24 '14 at 15:50

2 Answers2

3

I see the output being printed when i use fflush(stdout)

while(true){
cout<<"["<<b.consume()<<"] ";
fflush(stdout)
}

Output:

Magnum@SimpleGuy:~ [52]$ ./a.out [1] [2] [3] [4] [5] [6] [7] [8]

SimpleGuy
  • 2,764
  • 5
  • 28
  • 45
  • 1
    This would be what the [**`std::flush`**](http://en.cppreference.com/w/cpp/io/manip/flush) io-manipulator is for. – WhozCraig Nov 24 '14 at 06:57
  • @sonu kumar: If above solved your problem, you may accept the answer, so that others can be benefited too ! – SimpleGuy Nov 24 '14 at 07:01
  • 1
    cout<<"["< – Rush Nov 24 '14 at 07:03
  • @SimpleGuy I never encounter this type of problem in past. You have any idea why it happened? – sonus21 Nov 24 '14 at 07:36
  • @sonu kumar : `fflush` is used to flush a buffer (stdout) in your case. If you did not encounter the problem in the past, then your buffer was automatically flushed in the past. Many a times it is flushed automatically (say a criteria can be pushing on the basis of buffer being full). So, in past cases that would have been met – SimpleGuy Nov 24 '14 at 08:41
-1

How long did you try running the program?
Did you read the documentation for rand and sleep.
Rand function Link

Sleep function link

It could be happening that rand returns a big number every time and your producer after producing something first time goes for sleep for a long time, and during this time your consumer simply waits on cond_wait.

Trying using smaller sleep interval 'usleep' or something similar and you will see that your program works just fine.

milli/micro second sleep function link

Community
  • 1
  • 1
Rush
  • 486
  • 2
  • 11
  • I did not downvote, but your statement `It could be happening that rand returns a big number every time` can't be true as it is `rand()%2` so sleep can't be more than `0 or 1` – SimpleGuy Nov 24 '14 at 07:00
  • It wasn't me, but probably because there's `%2` in the commented out line. – kec Nov 24 '14 at 07:00
  • My bad! I missed that. Thanks for the comment. I did actually run it putting endl at the end of the statement and it runs fine. – Rush Nov 24 '14 at 07:01
  • @Rush When i run this program it takes more time than it should be to print output.Any way i didn't downvote.After adding fflush , works fine. – sonus21 Nov 24 '14 at 17:24