
Dans ce tutoriel, nous discuterons d'un programme pour comprendre les fonctions de thread en C/C++.
Les fonctions de threading permettent aux utilisateurs d'implémenter simultanément des fonctions concurrentes, qui peuvent dépendre les unes des autres pour l'exécution ou être indépendantes.
Exemple
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 | # include <pthread.h>
# include <stdio.h>
# include <stdlib.h>
void* func(void* arg){
pthread_detach(pthread_self());
printf( "Inside the thread\n" );
pthread_exit(NULL);
}
void fun(){
pthread_t ptid;
pthread_create(&ptid, NULL, &func, NULL);
printf( "This line may be printed before thread terminates\n" );
if (pthread_equal(ptid, pthread_self())
printf( "Threads are equal\n" );
else
printf( "Threads are not equal\n" );
pthread_join(ptid, NULL);
printf( "This line will be printed" " after thread ends\n" );
pthread_exit(NULL);
}
int main(){
fun();
return 0;
}
|
Copier après la connexion
Sortie
1 2 3 4 | This line may be printed before thread terminates
Threads are not equal
Inside the thread
This line will be printed after thread ends
|
Copier après la connexion
Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!