38,789
edits
(Created page with "=Pour en savoir plus= Pour plus d'information sur pthreads, sur les arguments optionnels pour les diverses fonctions (les paramètres utilisés dans cette page utilisent l'ar...") |
(Updating to match new version of source page) |
||
Line 194: | Line 194: | ||
In the above example we have two worker threads which modify the value of the integer <tt>workload</tt>, whose initial value must be less than or equal to 25. The first thread locks the mutex and then waits because <tt>workload <= 25</tt>, creating the condition variable <tt>ticker</tt> and releasing the mutex. The second thread can then perform a loop that increments the value of <tt>workload</tt> by three at each iteration. After each increment the second thread checks if the <tt>workload</tt> is greater than 25, and when it is, calls <tt>[http://pubs.opengroup.org/onlinepubs/007908799/xsh/pthread_cond_signal.html pthread_cond_signal]</tt> to alert the thread waiting on <tt>ticker</tt> that the condition is now satisfied. If there were more than one thread waiting on <tt>ticker</tt> we could instead use <tt>[http://pubs.opengroup.org/onlinepubs/009695399/functions/pthread_cond_broadcast.html pthread_cond_broadcast]</tt> to notify ''all'' waiting threads that the condition is satisfied. With the first thread signalled, the second thread sets the exit condition for the loop, releases the mutex, and disappears in the <tt>pthread_join</tt>. Meanwhile the first thread, having been woken up, increments <tt>workload</tt> by 15 and exits the function <tt>task</tt> itself. After the worker threads have been absorbed, the master thread prints out the final value of <tt>workload</tt> and the program exits. | In the above example we have two worker threads which modify the value of the integer <tt>workload</tt>, whose initial value must be less than or equal to 25. The first thread locks the mutex and then waits because <tt>workload <= 25</tt>, creating the condition variable <tt>ticker</tt> and releasing the mutex. The second thread can then perform a loop that increments the value of <tt>workload</tt> by three at each iteration. After each increment the second thread checks if the <tt>workload</tt> is greater than 25, and when it is, calls <tt>[http://pubs.opengroup.org/onlinepubs/007908799/xsh/pthread_cond_signal.html pthread_cond_signal]</tt> to alert the thread waiting on <tt>ticker</tt> that the condition is now satisfied. If there were more than one thread waiting on <tt>ticker</tt> we could instead use <tt>[http://pubs.opengroup.org/onlinepubs/009695399/functions/pthread_cond_broadcast.html pthread_cond_broadcast]</tt> to notify ''all'' waiting threads that the condition is satisfied. With the first thread signalled, the second thread sets the exit condition for the loop, releases the mutex, and disappears in the <tt>pthread_join</tt>. Meanwhile the first thread, having been woken up, increments <tt>workload</tt> by 15 and exits the function <tt>task</tt> itself. After the worker threads have been absorbed, the master thread prints out the final value of <tt>workload</tt> and the program exits. | ||
<div class="mw-translate-fuzzy"> | |||
=Pour en savoir plus= | =Pour en savoir plus= | ||
Pour plus d'information sur pthreads, sur les arguments optionnels pour les diverses fonctions (les paramètres utilisés dans cette page utilisent l'argument par défaut NULL) et sur les sujets de niveau avancé, nous recommandons l'ouvrage de David Butenhof, [https://ptgmedia.pearsoncmg.com/images/9780201633924/samplepages/0201633922.pdf Programming with POSIX Threads] ou l'excellent [https://computing.llnl.gov/tutorials/pthreads tutoriel du Lawrence Livermore National Laboratory]. | Pour plus d'information sur pthreads, sur les arguments optionnels pour les diverses fonctions (les paramètres utilisés dans cette page utilisent l'argument par défaut NULL) et sur les sujets de niveau avancé, nous recommandons l'ouvrage de David Butenhof, [https://ptgmedia.pearsoncmg.com/images/9780201633924/samplepages/0201633922.pdf Programming with POSIX Threads] ou l'excellent [https://computing.llnl.gov/tutorials/pthreads tutoriel du Lawrence Livermore National Laboratory]. | ||
</div> |