在同一线程中多次锁定互斥锁
我正在嵌入式 Linux 操作系统 (uClinux) 上开发一个应用程序,并且我需要能够多次锁定互斥体(通过同一线程)。
我有一个互斥锁和一个互斥锁,定义和初始化如下:
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);
但是当我尝试两次获取锁时,它会在第二个锁上阻塞:
pthread_mutex_lock(&waiting_barcode_mutex);
pthread_mutex_lock(&waiting_barcode_mutex);
我初始化它是错误的还是有更好的方法来完成相同的任务?
提前致谢。
结论:
- 显然 PTHREAD_MUTEX_RECURSIVE 或 PTHREAD_MUTEX_RECURSIVE_NP 不起作用,所以我无法创建可重入互斥体。
- try_lock 也不好。如果可以的话它会获取锁,如果无法获取锁则返回错误。不幸的是,该错误只是告诉我互斥体已在使用中,并且我无法确定当前线程是否已拥有该锁。
- 如果当前线程拥有锁,pthread_mutex_lock 可能会返回错误,但为此我需要创建一个 PTHREAD_MUTEX_ERRORCHECK 类型的互斥锁,但我也无法创建一个。
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.
Conclusions:
- Apparently PTHREAD_MUTEX_RECURSIVE or PTHREAD_MUTEX_RECURSIVE_NP don't work so I can't create a reentrant mutex.
- 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 already owns the lock or not.
- pthread_mutex_lock can return an error if the current thread has the lock but for that I need to create a mutex of the type PTHREAD_MUTEX_ERRORCHECK, and I can't create one either.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这不是你所期望的吗?
第一个调用获取锁,第二个调用将阻塞,直到第一个锁被释放 (
pthread_mutex_unlock
)。这就是锁的作用。从文档中可以看出:
“如果互斥体已经被锁定,则调用线程会阻塞,直到互斥体变得可用。”
也许您想要
pthread_mutex_trylock
?除非我们知道您想要实现什么目标,否则很难说。更正:
我没有看到您正在设置 PTHREAD_MUTEX_RECURSIVE...让我再考虑一下。
思考后:
通过谷歌代码搜索,看起来 PTHREAD_MUTEX_RECURSIVE 并未在所有库中实现。您可以尝试 PTHREAD_MUTEX_RECURSIVE_NP,或者您可能做了一些奇特的事情来解决这个问题。
Isn't this doing what you would expect?
The first call acquires the lock, and the second one will block until the first lock is released (
pthread_mutex_unlock
). This is what locks do.From the documentation:
"If the mutex is already locked, the calling thread blocks until the mutex becomes available."
Perhaps you want
pthread_mutex_trylock
? It's hard to say unless we know what you are trying to accomplish.CORRECTION:
I didn't see that you were setting PTHREAD_MUTEX_RECURSIVE.... Let me think about this some more.
AFTER THINKING:
From poking around google codesearch, it looks like PTHREAD_MUTEX_RECURSIVE is not implemented in all libs. You may try PTHREAD_MUTEX_RECURSIVE_NP, or you may have do something fancy to get around this.
听起来 pthread 互斥锁不可重入。您可以使用一个标志来解决这个问题,该标志指示您的线程是否已经锁定了互斥体:
It sounds like the pthread mutex is not reentrant. You could work around this with a flag indicating if your thread already has locked the mutex:
(刚刚意识到我没有将此问题标记为已回答)
取自问题中的结论:
(Just realised I didn't mark this question as answered)
Taken from the Conclusions in the question:
以下是在我的 Dell m6300 上的 UBUNTU 12.04 LTS 上测试的工作代码:
不要忘记在获取互斥体时多次释放它。
Here is working code tested on UBUNTU 12.04 LTS on my Dell m6300:
Do not forget to release the mutex as many times as you acquired it.
输出:
OUTPUT: