Skip to content

Instantly share code, notes, and snippets.

@chrispian
Created August 6, 2019 20:36
Show Gist options
  • Save chrispian/2c2f9fc1ceb82dc8de9b7ccf23a618f8 to your computer and use it in GitHub Desktop.
Save chrispian/2c2f9fc1ceb82dc8de9b7ccf23a618f8 to your computer and use it in GitHub Desktop.
Semaphore example
//Semaphore properties
$key = 1111; // Key can be any unique number
$max = 1; // Max number of connections
$permissions = 0666; // default permissions
$autoRelease = 1; // To keep from a deadlock
// Begin Semaphore locking
$semaphore = sem_get( $key, $max, $permissions, $autoRelease );
if( !$semaphore ) {
// Probably best to log vs. error message. But should exit or return to prevent execution if we can't get the semaphore.
echo "Something went wrong. Could not access semaphore.\n";
exit;
}
sem_acquire($semaphore);
// If we get to here then the below code will execute.
// Code to execute goes here
// Release the semaphore lock so the next process can run.
sem_release($semaphore);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment