 
	#include <threads.h>
/* Statuts d'erreurs et de sortie. */
enum {     
    thrd_success  = 0,
    thrd_busy     = 1,
    thrd_error    = 2,
    thrd_nomem    = 3,
    thrd_timedout = 4
}                
            Cette fonction énumération non nommées définit cinq constantes permettant de représenter des status de sortie de fonction et d'erreurs. Voici la signification de chaque constante.
| Nom | Description | 
|---|---|
| thrd_busy | Associée à une erreur due à une ressource temporairement non disponible. | 
| thrd_error | Associée à une erreur, sans plus de précision. | 
| thrd_nomem | Indique une erreur pour cause de manque de mémoire. | 
| thrd_success | Associée à une sortie en succés. | 
| thrd_timeout | Associée à une erreur de dépassement de délai. | 
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | #include <stdio.h> #include <stdlib.h> #include <threads.h> // La fonction sur laquelle va démarrer notre thread int threadMain( void * data ) { const char * threadName = (const char *) data; for ( int i=0; i<1000000; i++ ) { printf( "%s - %d\n", threadName, i ); } printf( "My thread is terminated\n" ); return thrd_success; } int main() { // On crée un thread. thrd_t myThread; void * threadName = (void *) "My thread"; if ( thrd_create( &myThread, threadMain, threadName ) != thrd_success ) { fprintf( stderr, "Cannot create thread\n" ); return EXIT_FAILURE; } // On attent la fin d'exécution du thread int threadResultCode; if ( thrd_join( myThread, &threadResultCode ) == thrd_error ) { fprintf( stderr, "Impossible d'attendre le thread\n" ); return EXIT_FAILURE; } if ( threadResultCode != thrd_success ) { fprintf( stderr, "Notre thread ne s'est pas correctement terminé.\n" ); return EXIT_FAILURE; } // On sort du programme. printf( "Le thread initial/principal s'arrête.\n" ); return EXIT_SUCCESS; } | 
Pour compiler cet exemple sous environnement Linux/Unix, il est nécessaire de lier la librairie pthread (Posix Thread) à votre exécutable. Voici un exemple de compilation.
$> gcc -o sample sample.c -lpthread $>
 
	
Améliorations / Corrections
Vous avez des améliorations (ou des corrections) à proposer pour ce document : je vous remerçie par avance de m'en faire part, cela m'aide à améliorer le site.
Emplacement :
Description des améliorations :