Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- bool woodPileEmpty = false;
- bool woodPileBusy = false;
- int amountWoodInPile = 10;
- Semafore mutexWoodInPile, mutexWoodPileEmpty;
- void stoker(){
- while(1){
- if(!woodPileBusy){
- woodPileBusy = true;
- mutexWoodInPile.wait();
- if(amountWoodInPile > 0){
- amountWoodInPile -= 1; // Adding wood to fire
- } else {
- woodPileEmpty = true;
- woodPileBusy = false;
- mutexWoodInPile.signal();
- suspend();
- }
- woodPileBusy = false;
- }
- }
- }
- void stacker(){
- while(1){
- if(!woodPileBusy){
- woodPileBusy = true;
- mutexWoodPileEmpty.wait();
- if(woodPileEmpty){
- amountWoodInPile = 10; // Refilling woodpile
- resume(stoker);
- woodPileEmpty = false;
- }
- mutexWoodPileEmpty.signal();
- woodPileBusy = false;
- }
- }
- The semaphores will protect the critical region, which is the IF statements, thus defendig it from data races.
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement