Pthreads/fr: Difference between revisions

Created page with "Dans cet exemple, l'index du fil (de 0 à 11) est passé en argument; la fonction <tt>task</tt> est donc exécutée par chacun des 12 fils. Remarquez que la fonction <tt>pth..."
No edit summary
(Created page with "Dans cet exemple, l'index du fil (de 0 à 11) est passé en argument; la fonction <tt>task</tt> est donc exécutée par chacun des 12 fils. Remarquez que la fonction <tt>pth...")
Line 57: Line 57:
}
}
}}
}}
This simple example creates twelve threads, each one executing the function <tt>task</tt> with the argument consisting of the thread's index, from 0 to 11. Note that the call of <tt>pthread_create</tt> is non-blocking, i.e. the root or master thread, which is executing the <tt>main</tt> function, continues to execute after each of the twelve worker threads is created. After creating the twelve threads, the master thread then goes into the second ''for'' loop and calls <tt>[http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_join.html pthread_join]</tt>, a blocking function where the master thread waits for the twelve workers to finish executing the function <tt>task</tt> and rejoin the master thread. While trivial, this program illustrates the basic lifecycle of a POSIX thread: the master thread creates a thread by assigning it a function to run, then waits for the thread to finish and join back into the execution of the master thread.
Dans cet exemple, l'index du fil (de 0 à 11) est passé en argument;  la fonction <tt>task</tt> est donc exécutée par chacun des 12 fils. Remarquez que  la fonction <tt>pthread_create</tt> ne bloque pas le  fil maître, qui continue à exécuter la fonction <tt>main</tt> après la création de chacun des fils. Une fois les 12 fils créés, le fil maître entre dans la deuxième boucle ''for'' et appelle la fonction bloquante <tt>[http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_join.html pthread_join]</tt> : le fil maître attend alors que les 12 fils esclaves terminent l'exécution de la fonction <tt>task</tt> et qu'ils réintègrent ensuite le fil maître.


If you run this test program several times in a row you'll likely notice that the order in which you see the various worker threads saying hello varies, which is what we would expect since they are now running in an asynchronous manner. Each time the program is executed, the twelve threads compete for access to the standard output during the <tt>printf</tt> call and from one execution of the program to another the winners of this competition will change.
If you run this test program several times in a row you'll likely notice that the order in which you see the various worker threads saying hello varies, which is what we would expect since they are now running in an asynchronous manner. Each time the program is executed, the twelve threads compete for access to the standard output during the <tt>printf</tt> call and from one execution of the program to another the winners of this competition will change.
rsnt_translations
56,430

edits