0

I have a struck with an array of pthread pointers. Each thread is meant to read a different data stream

typedef struct {
  // ...other stuff
  pthread_t *threads[MAX_STREAM_COUNT];
} stream_manager;

And when I want to start reading:

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]       );
}

When I want to stop reading:

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) )
   }
}

For now, my read_stream_cb function simply prints the name of the stream its reading from.

void* read_stream_cb(stream *strm) {
   stream *s = (stream*) strm;
   prinf("%s\n", s->name);
}

In the main program, I initialize 2 streams with different names. I call run start_reading(), sleep(3) and stop_reading()). The program prints the stream names fine for 3 seconds, but the pthread_join does not return successfully. It returns 3 and prints out

pthread join error: Operation timed out
pthread_join returned with errer:: No such process

I think this may be a pointer issue? I may just be confused with order of operations with these pointers in the join pthread_join(*sm->streams[i], NULL); . I'll look into that more.

4

1 回答 1

3

pthread_create()以 apthread_t*作为参数,这是通过 a pthread_t**

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

就像sm->threads一个数组一样pthread_t*。更改stream_manager为:

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

并从对pthread_create().

pthread_join()需要一个pthread_t

pthread_join(sm->threads[i]); /* Not sm->streams[i]. */
于 2012-07-03T19:32:02.260 回答