/** * 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; }
/** * Reduce working producer count by 1. */ void producer_finished() { #pragma omp critical { producer_count -= 1; can_read.release(); } }
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(); } }
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(); } }
/** * Store book into storage. */ void store(book &b) { #pragma omp critical { bool saved = false; for (vector<book>::iterator it = books.begin(); it < books.end(); it++) { if (it->year == b.year) { debug << "found existing book " << it->title << ", incrementing from " << it->count << " to " << (it->count + b.count) << " by " << b.count << "\n"; it->count += b.count; saved = true; } } if (! saved) { debug << "adding new book " << b.title << "\n"; add_book(b); } can_read.release(); } }