@@ -24,10 +24,14 @@ Installation
24
24
Usage
25
25
-----
26
26
27
- Semaphore are used to guarantee exclusive access to some shared resource.
27
+ In computer science, a semaphore is a variable or abstract data type used to
28
+ control access to a common resource by multiple processes in a concurrent
29
+ system such as a multitasking operating system. The main difference
30
+ with :doc: `locks </lock >` is that semaphores allow more than one process to
31
+ access a resource, whereas locks only allow one process.
28
32
29
- Semaphore are created using a :class: `Symfony\\ Component\\ Semaphore\\ SemaphoreFactory ` class,
30
- which in turn requires another class to manage the storage of Semaphore ::
33
+ Create semaphores with the :class: `Symfony\\ Component\\ Semaphore\\ SemaphoreFactory `
34
+ class, which in turn requires another class to manage the storage::
31
35
32
36
use Symfony\Component\Semaphore\SemaphoreFactory;
33
37
use Symfony\Component\Semaphore\Store\RedisStore;
@@ -38,13 +42,12 @@ which in turn requires another class to manage the storage of Semaphore::
38
42
$store = new RedisStore($redis);
39
43
$factory = new SemaphoreFactory($store);
40
44
41
-
42
45
The semaphore is created by calling the
43
46
:method: `Symfony\\ Component\\ Semaphore\\ SemaphoreFactory::createSemaphore `
44
47
method. Its first argument is an arbitrary string that represents the locked
45
- resource. Its second argument is the number of process allowed. Then, a call to
46
- the :method: `Symfony\\ Component\\ Semaphore\\ SemaphoreInterface::acquire ` method
47
- will try to acquire the semaphore::
48
+ resource. Its second argument is the maximum number of process allowed. Then, a
49
+ call to the :method: `Symfony\\ Component\\ Semaphore\\ SemaphoreInterface::acquire `
50
+ method will try to acquire the semaphore::
48
51
49
52
// ...
50
53
$semaphore = $factory->createSemaphore('pdf-invoice-generation', 2);
@@ -62,7 +65,7 @@ already acquired.
62
65
63
66
.. note ::
64
67
65
- Unlike other implementations, the Semaphore Component distinguishes
68
+ Unlike other implementations, the Semaphore component distinguishes
66
69
semaphores instances even when they are created for the same resource. If a
67
70
semaphore has to be used by several services, they should share the same
68
71
``Semaphore `` instance returned by the ``SemaphoreFactory::createSemaphore ``
@@ -73,7 +76,6 @@ already acquired.
73
76
If you don't release the semaphore explicitly, it will be released
74
77
automatically on instance destruction. In some cases, it can be useful to
75
78
lock a resource across several requests. To disable the automatic release
76
- behavior, set the last argument of the ``createLock() `` method to
77
- ``false ``.
79
+ behavior, set the fifth argument of the ``createLock() `` method to ``false ``.
78
80
79
81
.. _`semaphores` : https://en.wikipedia.org/wiki/Semaphore_(programming)
0 commit comments