Relative Content

Tag Archive for c++pthreads

How to handle error creating threads in a loop

I’m into the Philosophers project, it is about handling threads and mutexes. For the project I need to create several threads, each thread runs a routine function and accesses shared resources. I discovered that when I test pthread_create() != 0 it causes a broad set of errors, from data race to segfault. When there is a thread error, I want to exit the loop, clean all resources and join created threads, but as they use mutexes I can not free locked values. I was thinking about using pthread_detach but not sure how to do it right.

How to handle error creating threads in a loop

I’m into the Philosophers project, it is about handling threads and mutexes. For the project I need to create several threads, each thread runs a routine function and accesses shared resources. I discovered that when I test pthread_create() != 0 it causes a broad set of errors, from data race to segfault. When there is a thread error, I want to exit the loop, clean all resources and join created threads, but as they use mutexes I can not free locked values. I was thinking about using pthread_detach but not sure how to do it right.

problem using pthread_cond_signal and pthread_cond_timedwait

I have a small program with two threads sending signal to each other.
The program first works as expected but stop after various runs as the pthread_cond_timedwait run into a timeout.
For my test I stop the program if one thread is running into a timeout.

consumer producer problem were the producer takes consumer that are busy

I have a producer consumer problem with PThreads.
The program has an array of threads. Each producer goes through the array and tries to lock a mutex. if the mutex is lockable, the corresponding thread is free. The problem is, without the usleep(1) producers use threads that are not free. with the usleep(1) it works as expected. I think there is a race condition and signaling a condition is not instant. But I am not sure and I don’t know what the best way to fix this is.