Pthreads/fr: Difference between revisions

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..."
No edit summary
(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...")
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.


=Further Reading=
=Pour en savoir plus=  
This page is only intended to provide a very brief overview of what is in fact a complex and demanding subject. Individuals who are interested in a more in-depth discussion of pthreads, the various optional arguments that are available for many function calls - where we have used the default NULL argument for such parameters in this page - and advanced topics can consult sources like David Butenhof's Programming with POSIX Threads (Addison-Wesley, 1993) or the excellent [https://computing.llnl.gov/tutorials/pthreads LANL tutorial].
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].
rsnt_translations
56,437

edits