I am a bit confused on how to declare a recursive mutex using pthread. What I try to do is have only one thread at a time be able to run a piece of code(including functions) but after scepticism I figured out that the use of mutexes would not work and that instead I should use recursive mutexes. Here is my code:
pthread_mutex_lock(&mutex); // LOCK
item = queue_peek(queue); // get last item in queue
item_buff=item; // save item to a buffer
queue_removelast(queue); // remove last item from queue
pthread_mutex_unlock(&mutex); // UNLOCK
So what I try to do is just read/remove from the queue serially.
The thing is that there isn't any example out there on how to declare recursive mutexes. Or there maybe a few but they don't compile for me.
mutex
is used for all threads that accessqueue
. For this reason, it would be usual to include the mutex within the queue itself:pthread_mutex_lock(&mutex->queue);
, or if the queue is an opaque data structure,queue_lock(queue);
(wherequeue_lock()
locks the mutex internally). – cafflockfile
. – R.. GitHub STOP HELPING ICE