php-ipc/examples/semaphores.php

21 lines
550 B
PHP
Raw Normal View History

2018-04-15 20:08:19 +00:00
<?php
require_once __DIR__."/../vendor/autoload.php";
use NoccyLabs\Ipc\Key\FileKey;
use NoccyLabs\Ipc\Sem\Semaphore;
$key = new FileKey(__FILE__);
// Create semaphore with max count of 2
$sem1 = new Semaphore($key, 2);
$sem2 = new Semaphore($key, 2);
$sem3 = new Semaphore($key, 2);
printf("sem1 acquire: %d\n", $sem1->acquire());
printf("sem2 acquire: %d\n", $sem2->acquire());
printf("sem3 acquire: %d\n", $sem3->acquire());
printf("sem2 release: %d\n", $sem2->release());
printf("sem3 acquire: %d\n", $sem3->acquire());
$sem1->destroy();