/**
   * Consume a book by filter.
   *
   * Return number of items consumed.
   */
  unsigned int consume(filter &f) {
    can_read.acquire();
    unsigned int consumed = 0;
    #pragma omp critical
    {
      book *b = find_by_year(f.year);
      if (b != NULL) {
        unsigned int wants_to_consume = f.get_wants_to_consume();
        if (b->count < wants_to_consume)
          consumed = b->count;
        else
          consumed = wants_to_consume;

        b->count -= consumed;
        if (b->empty()) {
          debug << "Deleting " << b->title << endl;
          remove(b);
        }

        f.consume(consumed);
      }
      else {
        can_read.release();
      }
    }
    return consumed;
  }
Ejemplo n.º 2
0
void Make(int add) {
	int used;
	while(!doneUsing){
		S.acquire();
		if((howMany % 2 == 0 && howMany != used) || (howMany % 3 == 0 && howMany != used)){
			common += add;
			used = howMany;
		}
		S.release();
	}
	
}
Ejemplo n.º 3
0
void Use(int procNr) {
	int val;
	while(!doneUsing){
		S.acquire();
		if(howMany < 10 && val != common){
			cout << procNr << ") reiksme: " << common << endl;
			val = common;
			howMany++;
		} else if (howMany == 10) {
			doneUsing = true;
		}
		S.release();
	}
	
}