Skip to content

Instantly share code, notes, and snippets.

@finagin
Created July 17, 2018 08:38
Show Gist options
  • Save finagin/fcda0c923edff691f26a556e26405f30 to your computer and use it in GitHub Desktop.
Save finagin/fcda0c923edff691f26a556e26405f30 to your computer and use it in GitHub Desktop.
MUTual EXclusion. Semaphore.
<?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";
});
@freelancerhabibkhan
Copy link

nice man

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment