2012-07-03 2 views
0

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.

+1

Est-il 'SM-> streams' ou' SM-> threads'? – cnicutar

+0

vous mettez '(void *) sm-> streams [i]' dans 'pthread_join()'?! – Tobas

Répondre

3

pthread_create() prend un pthread_t* comme argument, ce passe un pthread_t**:

pthread_create((pthread*) &sm->threads[i], 

comme sm->threads est un tableau de pthread_t*. Changer stream_manager à:

typedef struct { 
    // ...other stuff 
    pthread_t threads[MAX_STREAM_COUNT]; /* No longer array of pointers. */ 
} stream_manager; 

et enlever la distribution inutile de l'appel à pthread_create().

pthread_join() prend un pthread_t:

pthread_join(sm->threads[i]); /* Not sm->streams[i]. */