I’m currently working on a homework assignment for my university. We are supposed to make a circular buffer threadsafe, given a struct rbuf with two mutexes and two signals. The two mutexes can possibly be traced back to the fact that two writer and two reader threads are always active. The read and write functions work very well so far, as all tests without testing the thread safety beforehand were successful. They are basically only responsible for writing to the buffer and reading from the buffer. Also we should use pthread_cond_timedwait
. My current implementation always has data races (I know this thanks to the ThreadSanitizer) and unfortunately I have absolutely no idea how to synchronize this better.
This is my code so far, what am doing from this makes perfectly sense to me:
/*
typedef struct {
uint8_t* read;
uint8_t* write;
uint8_t* begin;
uint8_t* end;
pthread_mutex_t mutex_read;
pthread_mutex_t mutex_write;
pthread_cond_t signal_read;
pthread_cond_t signal_write;
} rbuf;
*/
int producer(rbuf *buffer, void *message, size_t message_len) {
int val;
struct timespec to;
while (1) {
pthread_mutex_lock(&buffer->mutex_write);
clock_gettime(CLOCK_REALTIME, &to);
to.tv_sec += 1;
retval = write(buffer, message, message_len);
if (val == 1 //SUCCESS) {
pthread_cond_signal(&buffer->signal_read);
pthread_mutex_unlock(&buffer->mutex_write);
return retval;
}
if (pthread_cond_timedwait(&buffer->signal_write, &buffer->mutex_write, &to) == ETIMEDOUT) {
pthread_mutex_unlock(&buffer->mutex_write);
return val;
}
}
}
int consumer(rbuf *buffer, void *buffer_read, size_t *buffer_len) {
int val;
struct timespec to;
while (1) {
pthread_mutex_lock(&buffer->mutex_read);
clock_gettime(CLOCK_REALTIME, &to);
to.tv_sec += 1;
retval = read(buffer, buffer_read, buffer_len);
if (val == 1 //SUCCESS) {
pthread_cond_signal(&buffer->signal_write);
pthread_mutex_unlock(&buffer->mutex_read);
return retval;
}
if (pthread_cond_timedwait(&buffer->signal_read, &buffer->mutex_read, &to) == ETIMEDOUT) {
pthread_mutex_unlock(&buffer->mutex_read);
return val;
}
pthread_mutex_unlock(&buffer->mutex_read);
}
}
``
I have tried several implemtations but this one is by far the best. A producer writes and sends the signal when writing was successful. The consumer does the same. The two functions are in a while loop because that made sense to me, but I don’t know if that’s right.I would be very happy about suggestions for improvement or advice on how i can solve this better. thank you in advance for your answers.
Kind regards!
Miriman is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.