Pthreads/fr: Difference between revisions

Created page with "Une synchronisation plus subtile est possible avec le verrou lecture/écriture <tt>pthread_rwlock_t</tt>. Cet outil permet la lecture simultanée d'une variable par plusieurs..."
No edit summary
(Created page with "Une synchronisation plus subtile est possible avec le verrou lecture/écriture <tt>pthread_rwlock_t</tt>. Cet outil permet la lecture simultanée d'une variable par plusieurs...")
Line 113: Line 113:
Dans le cas de l'alternative non bloquante <tt>[http://pubs.opengroup.org/onlinepubs/7908799/xsh/pthread_mutex_lock.html pthread_mutex_trylock]</tt>, la valeur non nulle est immédiatement produite si le mutex n'est pas accompli, indiquant ainsi que le mutex est occupé. Il faut aussi s'assurer qu'il n'y a pas de code superflu à l'intérieur du bloc sérialisé; puisque ce code est exécuté en série, il se doit d'être le plus concis possible afin de ne pas nuire au parallélisme dans l'exécution du programme.
Dans le cas de l'alternative non bloquante <tt>[http://pubs.opengroup.org/onlinepubs/7908799/xsh/pthread_mutex_lock.html pthread_mutex_trylock]</tt>, la valeur non nulle est immédiatement produite si le mutex n'est pas accompli, indiquant ainsi que le mutex est occupé. Il faut aussi s'assurer qu'il n'y a pas de code superflu à l'intérieur du bloc sérialisé; puisque ce code est exécuté en série, il se doit d'être le plus concis possible afin de ne pas nuire au parallélisme dans l'exécution du programme.


A more subtle form of data synchronization is possible with the read/write lock, <tt>pthread_rwlock_t</tt>. With this construct, multiple threads can simultaneously read the value of a variable but for write access, the read/write lock behaves like the standard mutex, i.e. no other thread may have have any access (read or write) to the variable. Like with a mutex, a <tt>pthread_rwlock_t</tt> must be initialized before its first use and destroyed when it is no longer needed during the program. Individual threads can obtain either a read lock by calling <tt>[http://pubs.opengroup.org/onlinepubs/7908799/xsh/pthread_rwlock_rdlock.html pthread_rwlock_rdlock]</tt>, or a write lock with <tt>[http://pubs.opengroup.org/onlinepubs/007908775/xsh/pthread_rwlock_wrlock.html pthread_rwlock_wrlock]</tt>. Either one is released using <tt>[http://pubs.opengroup.org/onlinepubs/7908799/xsh/pthread_rwlock_unlock.html pthread_rwlock_unlock]</tt>.
Une synchronisation plus subtile est possible avec le verrou lecture/écriture <tt>pthread_rwlock_t</tt>. Cet outil permet la lecture simultanée d'une variable par plusieurs fils, mais se comporte comme un mutex standard, c'est-à-dire qu'aucun autre fil n'a accès à cette variable (en lecture ou en écriture). Comme pour le mutex, le verrou <tt>pthread_rwlock_t</tt> doit être initialisé avant son utilisation et détruit quand il n'est plus nécessaire dans le cours du programme. Un fil obtient un verrou en lecture avec  <tt>[http://pubs.opengroup.org/onlinepubs/7908799/xsh/pthread_rwlock_rdlock.html pthread_rwlock_rdlock]</tt> et un verrou en écriture avec <tt>[http://pubs.opengroup.org/onlinepubs/007908775/xsh/pthread_rwlock_wrlock.html pthread_rwlock_wrlock]</tt>. Dans les deux cas, le verrou est détruit avec <tt>[http://pubs.opengroup.org/onlinepubs/7908799/xsh/pthread_rwlock_unlock.html pthread_rwlock_unlock]</tt>.


Another construct is used to allow multiple threads to wait for a single condition, for example waiting for work to become available for the worker threads. This construct is called a ''condition variable'' and has the datatype <tt>pthread_cond_t</tt>. Like a mutex or read/write lock, a condition variable must be initialized before its first use and destroyed when it is no longer needed. The use of a condition variable also requires a mutex to control access to the variable(s) that are the basis for the condition that is being tested. A thread that needs to wait on a condition will lock the mutex and then call the function <tt>[http://pubs.opengroup.org/onlinepubs/007908775/xsh/pthread_cond_wait.html pthread_cond_wait]</tt> with two arguments: the condition variable, and the mutex. The mutex will be released ''atomically'' with the creation of the condition variable that the thread is now waiting upon, so that other threads can lock the mutex either to wait on the same condition or to modify one or more variables, thereby changing the condition.
Another construct is used to allow multiple threads to wait for a single condition, for example waiting for work to become available for the worker threads. This construct is called a ''condition variable'' and has the datatype <tt>pthread_cond_t</tt>. Like a mutex or read/write lock, a condition variable must be initialized before its first use and destroyed when it is no longer needed. The use of a condition variable also requires a mutex to control access to the variable(s) that are the basis for the condition that is being tested. A thread that needs to wait on a condition will lock the mutex and then call the function <tt>[http://pubs.opengroup.org/onlinepubs/007908775/xsh/pthread_cond_wait.html pthread_cond_wait]</tt> with two arguments: the condition variable, and the mutex. The mutex will be released ''atomically'' with the creation of the condition variable that the thread is now waiting upon, so that other threads can lock the mutex either to wait on the same condition or to modify one or more variables, thereby changing the condition.
rsnt_translations
56,430

edits