Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * While this is not a 100% proof solution for critical sections
- * it does demonstrate how a mutex could work.
- *
- */
- $foo = 0;
- $locked = false; // This is our mutex (mutual exclusion) lock.
- function a() {
- global $foo;
- global $locked;
- // Wait till the other thread finishes (blocking!):
- while($locked) { /* Do nothing (we call this busy-waiting) */ }
- $locked = true; // This will trigger the while loop for the other thread.
- $foo = $foo + 2;
- $locked = false;
- }
- function b() {
- global $foo;
- global $locked;
- // Wait till the other thread finishes (blocking!):
- while($locked) { /* Do nothing (we call this busy-waiting) */ }
- $locked = true; // This will trigger the while loop for the other thread.
- $foo = $foo + 2;
- $locked = false;
- }
- print $foo;
- ?>
Advertisement
Add Comment
Please, Sign In to add comment