Bonjour,

J'aurais besoin de confirmations (ou pas!) de votre part sur le threads.

J'ai donc le code suivant :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
 
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
 
void thread_start()
{
        char *p = NULL;
 
        // p = ma_fonction_qui_retourne_un_char*
        printf("bonjour\n");
 
        pthread_exit(EXIT_SUCCESS);
}
 
int main()
{
        pthread_t *th;
        int i;
 
        if( (th = calloc(2, sizeof(pthread_t))) == NULL)
        {
                printf("Cannot allocate memory for threads");
                exit(EXIT_FAILURE);
        }
 
 
        for(i=0; i<2; i++)
        {
                printf("Thread %d\n", i);
 
                if( ( pthread_create(&th[i], NULL, (void *(*)())thread_start, NULL) ) == -1 )
                {
                        printf("cannot launch a thread");
                        exit(EXIT_FAILURE);
                }
        }
        //waiting threads
        for(i=0; i<2; i++)
                pthread_join(th[i], NULL);
 
        return EXIT_SUCCESS;
}
Ma question est la suivante : quelle est la portée de la variable *p dans thread_start() ? Est-ce que chaque thread à son propre p, est-ce qu'il est partagé entre les threads ? etc.
Egalement, si je fait pointer *p grace à une fonction qui renvoie un char*, est-ce que l'adresse "remplie" par la fonction est unique ou est-ce que chaque thread possède sa propre "zone mémoire pour la fonction" ?

En espérant avoir été assez clair =)

Merci