arizonalasas.blogg.se

Pthread c return
Pthread c return




pthread c return pthread c return

You pass the threads pointers to buffer and context, which are allocated on the stack inside main. When you pass a thread a pointer to a variable, you need to ensure that the lifetime of that variable is at least as long as the thread will attempt to access that variable. Make sure that all student threads have finished Pthread_create(&thread,NULL,Student,NULL) // Threads

Pthread c return code#

If that is the case, how could I handle the case where plenty of identical threads (like in the code below) would be created using the same thread identifier? In that case, how can I make sure that all the threads will have finished before main exits? Do I really have to keep an array of NUM_STUDENTS pthread_t identifiers to be able to do this? I guess I could do this by letting the Student threads signal a semaphore and then let the main function wait on that semaphore, but is there really no easier way to do this? int main() Pthread_join(writer,NULL) // This line seems to be necessary Pthread_join(reader,NULL) // This line seems to be necessary Status = pthread_create(&writer, NULL, Writer, &context)

pthread c return

Status = pthread_create(&reader, NULL, Reader, &context) Status = sem_init(&context.empty,0, BUFFER_SIZE) Status = pthread_mutex_init(&context.mutex, NULL) To guarantee that main will not exit before all running threads have finished, is it necessary to call pthread_join explicitly for all threads initialized directly in main? #include Ĭhar c = context->buffer Ĭontext->buffer = 'a' + (rand() % 26) įloat ranFloat = (float) rand() / RAND_MAX However when I include these two pthread_join function calls at the end of main it runs as it should. However when I run this code below without calling the two pthread_join functions (at the end of main) explicitly I get a segmentation fault, which seems to happen because the main function has been exited before the two threads finish their job, and therefore the char buffer is not available anymore. For some reason I thought that calling pthread_exit(NULL) at the end of a main function would guarantee that all running threads (at least created in the main function) would finish running before main could exit.






Pthread c return