Lock a mutex multiple times in the same thread

Posted by Megacan on Stack Overflow See other posts from Stack Overflow or by Megacan
Published on 2010-05-12T17:34:17Z Indexed on 2010/05/12 17:44 UTC
Read the original article Hit count: 179

Filed under:
|
|
|
|

Hi,

I'm developing an application on an embedded linux OS (uClinux) and I need to be able to lock the mutex more than once (by the same thread).

I have a mutex and a mutexattr defined and initialized as follows:

pthread_mutexattr_t waiting_barcode_mutexattr;
pthread_mutex_t waiting_barcode_mutex;

pthread_mutexattr_init(&waiting_barcode_mutexattr);
pthread_mutexattr_settype(&waiting_barcode_mutexattr, PTHREAD_MUTEX_RECURSIVE);
pthread_mutex_init(&waiting_barcode_mutex, &waiting_barcode_mutexattr);

But when I try to acquire the lock twice it blocks on the second lock:

pthread_mutex_lock(&waiting_barcode_mutex);
pthread_mutex_lock(&waiting_barcode_mutex);

Am I initializing it wrong or is there a better way of accomplishing the same?

Thanks in advance.

© Stack Overflow or respective owner

Related posts about c

    Related posts about embedded-linux