pthreads in C - pthread_exit -


for reason thought calling pthread_exit(null) @ end of main function guarantee running threads (at least created in main function) finish running before main exit. when run code below without calling 2 pthread_join functions (at end of main) explicitly segmentation fault, seems happen because main function has been exited before 2 threads finish job, , therefore char buffer not available anymore. when include these 2 pthread_join function calls @ end of main runs should. guarantee main not exit before running threads have finished, necessary call pthread_join explicitly threads initialized directly in main?

#include <stdlib.h> #include <stdio.h> #include <pthread.h> #include <unistd.h> #include <assert.h> #include <semaphore.h> #define num_char 1024 #define buffer_size 8  typedef struct {     pthread_mutex_t mutex;      sem_t full;     sem_t empty;     char* buffer; } context;  void *reader(void* arg) {     context* context = (context*) arg;     (int = 0; < num_char; ++i) {         sem_wait(&context->full);         pthread_mutex_lock(&(context->mutex));         char c = context->buffer[i % buffer_size];         pthread_mutex_unlock(&(context->mutex));         sem_post(&context->empty);          printf("%c", c);     }     printf("\n");     return null; }  void *writer(void* arg) {     context* context = (context*) arg;     (int = 0; < num_char; ++i) {         sem_wait(&context->empty);         pthread_mutex_lock(&(context->mutex));         context->buffer[i % buffer_size] = 'a' + (rand() % 26);         float ranfloat = (float) rand() / rand_max;         if (ranfloat < 0.5) sleep(0.2);         pthread_mutex_unlock(&(context->mutex));         sem_post(&context->full);     }     return null; }  int main() {     char buffer[buffer_size];     pthread_t reader, writer;     context context;     srand(time(null));     int status = 0;     status = pthread_mutex_init(&context.mutex, null);     status = sem_init(&context.full,0,0);     status = sem_init(&context.empty,0, buffer_size);     context.buffer = buffer;      status = pthread_create(&reader, null, reader, &context);     status = pthread_create(&writer, null, writer, &context);      pthread_join(reader,null);   // line seems necessary     pthread_join(writer,null);   // line seems necessary      pthread_exit(null);     return 0; } 

if case, how handle case plenty of identical threads (like in code below) created using same thread identifier? in case, how can make sure threads have finished before main exits? have keep array of num_students pthread_t identifiers able this? guess letting student threads signal semaphore , let main function wait on semaphore, there no easier way this?

int main() {     pthread_t thread;     (int = 0; < num_students; i++)         pthread_create(&thread,null,student,null);  // threads      // make sure student threads have finished     exit(0); } 

pthread_exit() function called thread terminate own execution. situation you've given not called main program thread.

as have figured out, pthread_join() correct means wait completion of joinable thread main().

also you've figured out, need maintain value returned pthread_create() pass pthread_join().

what means cannot use same pthread_t variable threads create if intend use pthread_join().

rather, build array of pthread_t have copy of each thread's id.


Comments

Popular posts from this blog

c++ - How do I get a multi line tooltip in MFC -

asp.net - In javascript how to find the height and width -

c# - DataTable to EnumerableRowCollection -