<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/book.swoole.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'fr',
  ),
  'this' => 
  array (
    0 => 'class.swoole-coroutine-lock.php',
    1 => 'Swoole\\Coroutine\\Lock',
    2 => 'La classe Swoole\\Coroutine\\Lock',
  ),
  'up' => 
  array (
    0 => 'book.swoole.php',
    1 => 'Swoole',
  ),
  'prev' => 
  array (
    0 => 'swoole-coroutine.suspend.php',
    1 => 'Swoole\\Coroutine::suspend',
  ),
  'next' => 
  array (
    0 => 'swoole-coroutine-lock.construct.php',
    1 => 'Swoole\\Coroutine\\Lock::__construct',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'fr',
    'path' => 'reference/swoole/swoole.coroutine.lock.xml',
  ),
  'history' => 
  array (
  ),
  'extra_header_links' => 
  array (
    'rel' => 'alternate',
    'href' => '/manual/en/feeds/class.swoole-coroutine-lock.atom',
    'type' => 'application/atom+xml',
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="class.swoole-coroutine-lock" class="reference">
 <h1 class="title">La classe Swoole\Coroutine\Lock</h1>
 

 <div class="partintro"><p class="verinfo">(No version information available, might only be in Git)</p>


  <div class="section" id="swoole-coroutine-lock.intro">
   <h2 class="title">Introduction</h2>
   <p class="para">
    Swoole 6.0.1 a introduit un verrou de coroutine qui prend en charge le partage entre processus et thread.
    Ce verrou est conçu pour un comportement non-bloquant et permet une synchronisation efficace des coroutines
    dans des environnements multi-processus et multi-thread.
   </p>
   <p class="para">
    Lorsque compilé avec l&#039;option <code class="literal">--enable-iouring</code> et que le noyau Linux prend en charge
    la fonctionnalité <code class="literal">io_uring futex</code>, le verrou de coroutine de Swoole utilise
    <code class="literal">io_uring futex</code> pour la synchronisation. Dans ce cas, les coroutines attendent pour la fin de verrouillage
    en utilisant un mécanisme de mise en file d&#039;attente efficace, ce qui améliore considérablement les performances.
   </p>
   <p class="para">
    Sans <code class="literal">io_uring futex</code>, le verrou de coroutine utilise un mécanisme de sommeil avec
    un backoff exponentiel, où le temps d&#039;attente augmente de 2^n millisecondes (n étant le nombre d&#039;échecs)
    après chaque tentative échouée d&#039;acquérir le verrou. Bien que cette approche évite l&#039;attente active, elle introduit
    une surcharge supplémentaire de planification CPU et une latence accrue.
   </p>
   <p class="para">
    Le verrou de coroutine est conçu pour être utilisé dans des environnements où les coroutines peuvent effectuer en toute sécurité 
    plusieurs opérations de verrouillage.
   </p>
   <div class="warning"><strong class="warning">Avertissement</strong>
    <p class="para">
     Ne pas créer de verrou dans une fonction de rappel telle que <code class="literal">onReceive</code>, car cela entraînera
     une croissance continue de la mémoire et conduira à des fuites de mémoire.
    </p>
   </div>
   <div class="warning"><strong class="warning">Avertissement</strong>
    <p class="para">
     Verrouiller et déverrouiller doivent être effectués dans la même coroutine, sinon cela rompra
     les conditions statiques.
    </p>
   </div>
  </div>


  <div class="section" id="swoole-coroutine-lock.synopsis">
   <h2 class="title">Synopsis de la classe</h2>


   <div class="classsynopsis">
    <span class="ooclass"><strong class="classname"></strong></span>


    <div class="classsynopsisinfo">
     <span class="ooclass">
      <span class="modifier">class</span> <strong class="classname">Swoole\Coroutine\Lock</strong>
     </span>
     {</div>


    <div class="classsynopsisinfo classsynopsisinfo_comment">/* Méthodes */</div>
    <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="methodname"><a href="swoole-coroutine-lock.construct.php" class="methodname">__construct</a></span>(): <span class="type"><a href="language.types.void.php" class="type void">void</a></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="swoole-coroutine-lock.lock.php" class="methodname">lock</a></span>(): <span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="swoole-coroutine-lock.trylock.php" class="methodname">trylock</a></span>(): <span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="swoole-coroutine-lock.unlock.php" class="methodname">unlock</a></span>(): <span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span></div>

   }</div>


  </div>

  <div class="section" id="swoole-coroutine-lock.examples">
   <h2 class="title">Exemples</h2>
   <div class="example" id="swoole-coroutine-lock.example.basic">
    <p><strong>Exemple #1 Utilisation basique</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">use </span><span style="color: #0000BB">Swoole\Coroutine\Lock</span><span style="color: #007700">;<br />use </span><span style="color: #0000BB">Swoole\Coroutine\WaitGroup</span><span style="color: #007700">;<br />use function </span><span style="color: #0000BB">Swoole\Coroutine\go</span><span style="color: #007700">;<br />use function </span><span style="color: #0000BB">Swoole\Coroutine\run</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$lock </span><span style="color: #007700">= new </span><span style="color: #0000BB">Lock</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$waitGroup </span><span style="color: #007700">= new </span><span style="color: #0000BB">WaitGroup</span><span style="color: #007700">();<br /><br /></span><span style="color: #0000BB">run</span><span style="color: #007700">(function() use (</span><span style="color: #0000BB">$lock</span><span style="color: #007700">, </span><span style="color: #0000BB">$waitGroup</span><span style="color: #007700">) {<br />    </span><span style="color: #0000BB">go</span><span style="color: #007700">(function() use (</span><span style="color: #0000BB">$lock</span><span style="color: #007700">, </span><span style="color: #0000BB">$waitGroup</span><span style="color: #007700">) {<br />        </span><span style="color: #0000BB">$waitGroup</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">add</span><span style="color: #007700">();<br />        </span><span style="color: #0000BB">$lock</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">lock</span><span style="color: #007700">();<br />        </span><span style="color: #0000BB">sleep</span><span style="color: #007700">(</span><span style="color: #0000BB">1</span><span style="color: #007700">);<br />        </span><span style="color: #0000BB">$lock</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">unlock</span><span style="color: #007700">();<br />        </span><span style="color: #0000BB">$waitGroup</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">done</span><span style="color: #007700">();<br />    });<br /><br />    </span><span style="color: #0000BB">go</span><span style="color: #007700">(function() use (</span><span style="color: #0000BB">$lock</span><span style="color: #007700">, </span><span style="color: #0000BB">$waitGroup</span><span style="color: #007700">) {<br />        </span><span style="color: #0000BB">$waitGroup</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">add</span><span style="color: #007700">();<br />        </span><span style="color: #0000BB">$lock</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">lock</span><span style="color: #007700">(); </span><span style="color: #FF8000">// Attend la coroutine qui détient le verrou<br />        </span><span style="color: #0000BB">sleep</span><span style="color: #007700">(</span><span style="color: #0000BB">1</span><span style="color: #007700">);<br />        </span><span style="color: #0000BB">$lock</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">unlock</span><span style="color: #007700">();<br />        </span><span style="color: #0000BB">$waitGroup</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">done</span><span style="color: #007700">();<br />    });<br /><br />    echo </span><span style="color: #DD0000">'Lock does not block the process'</span><span style="color: #007700">;<br />    </span><span style="color: #0000BB">$waitGroup</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">wait</span><span style="color: #007700">();<br />});</span></span></code></div>
    </div>

   </div>
  </div>

 </div>

 































<h2>Sommaire</h2><ul class="chunklist chunklist_reference"><li><a href="swoole-coroutine-lock.construct.php">Swoole\Coroutine\Lock::__construct</a> — Construit un nouveau verrou de coroutine</li><li><a href="swoole-coroutine-lock.lock.php">Swoole\Coroutine\Lock::lock</a> — Acquiert le verrou, en bloquant si n&eacute;cessaire</li><li><a href="swoole-coroutine-lock.trylock.php">Swoole\Coroutine\Lock::trylock</a> — Essaie d'acqu&eacute;rir le verrou sans bloquer</li><li><a href="swoole-coroutine-lock.unlock.php">Swoole\Coroutine\Lock::unlock</a> — Rel&acirc;che le verrou</li></ul>
</div>
<?php manual_footer($setup); ?>