Pages : 1
#1 Le 16/02/2016, à 20:59
- kboo
C et thread
Bonjour,
je cherche à comprendre un bout de code que voici:
#include <stdio.h>
#include <pthread.h>
int i;
void addition(void)
{
i = i + 10;
printf("hello thread fils i=%d\n", i);
i = i + 20;
printf("hello thread fils i=%d\n", i);
}
int main(void)
{
pthread_t num_thread;
i = 0;
if(pthread_create(&num_thread, NULL, (void *(*)())addition, NULL) == -1) perror("pb pthread_create\n");
i = i + 1000;
printf("thread principal %d\n", i);
i = i + 2000;
printf("thread principal %d\n", i);
pthread_join(num_thread, NULL);
}
si je comprend bien sur la fonction pthread_create on a en deuxième paramètre un pointeur de fonction... mais pourquoi (void *(*)()) devant?
je n'arrive pas à décortiquer
merci bien
Dernière modification par kboo (Le 16/02/2016, à 21:00)
Hors ligne
#2 Le 16/02/2016, à 23:26
- derderder
Re : C et thread
Il s'agit d'un simple cast ( inutile d'ailleurs) du type de la fonction, que l'on peut decortiquer par void* /* Nouveau type de retour de la fonction, pointeur générique */ (*) /* Pointeur de fonction */ () /* Arguments de la fonction, ici aucun */. La fonction passé a pthread_create sera donc void* addition(); Supprime ou ignore le, il est absolument inutile.
Hors ligne
#3 Le 16/02/2016, à 23:47
- kboo
Re : C et thread
merci derderder,
j'ai alors essayé ceci:
/*
============================================================================
Name : testC.c
Author : KbOo;)
Version :
Copyright : Your copyright notice
Description : Hello World in C, Ansi-style
============================================================================
*/
#include <stdio.h>
#include <pthread.h>
int i;
void addition(void)
{
i = i + 10;
printf("hello thread fils i=%d\n", i);
i = i + 20;
printf("hello thread fils i=%d\n", i);
}
int main(void)
{
pthread_t num_thread;
i = 0;
if(pthread_create(&num_thread, NULL, addition, NULL) == -1) perror("pb pthread_create\n");
i = i + 1000;
printf("thread principal %d\n", i);
i = i + 2000;
printf("thread principal %d\n", i);
pthread_join(num_thread, NULL);
}
donne ce résultat:
23:45:51 **** Incremental Build of configuration Debug for project testC ****
make all
Building file: ../src/testC.c
Invoking: GCC C Compiler
gcc -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/testC.d" -MT"src/testC.o" -o "src/testC.o" "../src/testC.c"
../src/testC.c:47:39: warning: incompatible pointer types passing 'void (void)' to parameter of type 'void *(*)(void *)' [-Wincompatible-pointer-types]
if(pthread_create(&num_thread, NULL, addition, NULL) == -1) perror("pb pthread_create\n");
^~~~~~~~
/usr/include/pthread.h:313:11: note: passing argument to parameter here
void *(*)(void *), void * __restrict);
^
1 warning generated.
Finished building: ../src/testC.c
Building target: testC
Invoking: C Linker
gcc -o "testC" ./src/testC.o
Finished building target: testC
23:45:53 Build Finished (took 1s.625ms)
Hors ligne
#4 Le 17/02/2016, à 17:19
- derderder
Re : C et thread
Laisse le alors, mais en soi c'est simplement pour virer le warming, il n'y a pas de réel intéret.
Hors ligne
Pages : 1