Bonjour
Bonjour
J'étais en train de m'informer pour la mise en place de threads sous
Linux. J'ai commencé par apprendre avec le tutoriel en anglais qui est
sur ce site : http://www.advancedlinuxprogramming.com/
Je suis arrivé à la gestion et la réservation des données par threads
avec la fonction pthread_key_create
J'ai compilé la source en rapport (Per-Thread Log Files Implemented with
Thread-Specific Data) et lorsque je l'éxécute j'ai une erreur de pile
lors de la sortie de la fonction thread_function.
Voici le code :
J'ai vu que vous avez fait un résumé de ce tutoriel à cette adresse : http://mtodorovic.developpez.com/lin...=page_4#L4-2-2.
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
45
46
47 #include <malloc.h> #include <pthread.h> #include <stdio.h> /* The key used to associate a log file pointer with each thread. */ static pthread_key_t thread_log_key; /* Write MESSAGE to the log file for the current thread. */ void write_to_thread_log (const char* message) { FILE* thread_log = (FILE*) pthread_getspecific (thread_log_key); fprintf (thread_log, %s\n, message); } /* Close the log file pointer THREAD_LOG. */ void close_thread_log (void* thread_log) { fclose ((FILE*) thread_log); } void* thread_function (void* args) { char thread_log_filename[20]; FILE* thread_log; /* Generate the filename for this threads log file. */ sprintf (thread_log_filename, thread%d.log, (int) pthread_self ()); /* Open the log file. */ thread_log = fopen (thread_log_filename, w); /* Store the file pointer in thread-specific data under thread_log_key. */ pthread_setspecific (thread_log_key, thread_log); write_to_thread_log (Thread starting.); /* Do work here... */ return NULL; } int main () { int i; pthread_t threads[5]; /* Create a key to associate thread log file pointers in thread-specific data. Use close_thread_log to clean up the file pointers. */ pthread_key_create (&thread_log_key, close_thread_log); /* Create threads to do the work. */ for (i = 0; i < 5; ++i) pthread_create (&(threads[i]), NULL, thread_function, NULL); /* Wait for all threads to finish. */ for (i = 0; i < 5; ++i) pthread_join (threads[i], NULL); return 0; }
Est-ce que quelqu'un pourrai le compiler ?
Pourriez vous me dire pourquoi j'ai ce problème?
D'avance Merci
Lann
Partager