2024年6月9日发(作者:)
本文由我司收集整编,推荐下载,如有疑问,请与我司联系
在同一个线程中多次锁定互斥锁
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).
我正在开发嵌入式 Linux 操作系统(uClinux)上的应用程序,我需要能够多次锁
定互斥锁(通过相同的线程)。
I have a mutex and a mutexattr defined and initialized as follows:
我有一个互斥锁和一个 mutexattr 定义和初始化如下:
pthread_mutexattr_t waiting_barcode_mutexattr;pthread_mutex_t
waiting_barcode_mutex;pthread_mutexattr_init(
waiting_barcode_mutexattr);pthread_mutexattr_settype(
PTHREAD_MUTEX_RECURSIVE);pthread_mutex_init(
waiting_barcode_mutexattr,
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.
提前致谢。
Conclusions: Apparently PTHREAD_MUTEX_RECURSIVE or
PTHREAD_MUTEX_RECURSIVE_NP don’t work so I can’t create a reentrant mutex.
显然 PTHREAD_MUTEX_RECURSIVE 或 PTHREAD_MUTEX_RECURSIVE_NP 不
起作用,因此我无法创建可重入的互斥锁。 try_lock is no good either. It acquires the
lock if it can and returns an error if it can’t acquire the lock. Unfortunately the error just
informs me that the mutex is already in use and I can´t find out if the current thread
发布评论