J'ai un frappé avec un tableau de pointeurs pthread. Chaque fil est destiné à lire un flux de données différentespthread tableau de pointeurs
typedef struct {
// ...other stuff
pthread_t *threads[MAX_STREAM_COUNT];
} stream_manager;
Et quand je veux commencer à lire:
void start_reading(stream_manager *sm, int i) {
// do some pre processing stuff
pthread_create((pthread*) &sm->threads[i],
NULL,
read_stream_cb,
(void*) sm->streams[i] );
}
Quand je veux arrêter la lecture:
void stop_reading(stream_manager *sm, int i) {
iret = pthread_join(*sm->threads[i], NULL);
if(iret != 0) {
perror("pthread_join returned with error:: ");
printf("pthread_join returned with error:: %s\n", strerror(iret))
}
}
Pour l'instant, mon La fonction read_stream_cb imprime simplement le nom du flux dont il lit la lecture.
void* read_stream_cb(stream *strm) {
stream *s = (stream*) strm;
prinf("%s\n", s->name);
}
Dans le programme principal, j'initialise 2 flux avec des noms différents. J'appelle lancer start_reading(), sleep (3) et stop_reading()). Le programme imprime les noms de flux pendant 3 secondes, mais le pthread_join ne retourne pas avec succès. Il retourne 3
et imprime
pthread join error: Operation timed out
pthread_join returned with errer:: No such process
Je pense que cela peut être un problème de pointeur? Je peux juste être confondu avec l'ordre des opérations avec ces pointeurs dans la jointure pthread_join(*sm->streams[i], NULL);
. Je vais regarder dans ce plus.
Est-il 'SM-> streams' ou' SM-> threads'? – cnicutar
vous mettez '(void *) sm-> streams [i]' dans 'pthread_join()'?! – Tobas