Created
July 17, 2018 08:38
-
-
Save finagin/fcda0c923edff691f26a556e26405f30 to your computer and use it in GitHub Desktop.
MUTual EXclusion. Semaphore.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php declare(strict_types=1); | |
function phone_semaphore($phone, callable $callback) | |
{ | |
$sem = sem_get($phone, 1); | |
if (sem_acquire($sem)) { | |
$result = $callback(); | |
sem_release($sem); | |
return $result; | |
} else { | |
throw new \Exception('Something went wrong..'); | |
} | |
} | |
$phone = 9999999999; | |
phone_semaphore($phone, function () { | |
echo 'start'."\n"; | |
sleep(5); | |
echo 'end'."\n"; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
nice man