boost中有没有一个函数可以查找有多少人正在等待互斥锁?
我使用的是boost 1.41.0。有没有办法使用boost来查找有多少人在等待锁?
I am using boost 1.41.0. Is there a way to use boost to find how many people are waiting on the lock?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
否:没有办法获得可靠的答案,因为它可能在函数返回之前发生变化,并且 Boost 的接口也不提供不可靠的方法。
如果你想要这样的东西,你需要在互斥锁旁边实现你自己的线程安全计数器;我不确定你是否可以可靠地做到这一点,但如果这足以满足你的需求,你可以很容易地区分“大约没有”、“一些”和“很多”。
No: there is no way to get a reliable answer as it could change before the function returns, and Boost's interface doesn't provide an unreliable method either.
If you want such a thing, you'll need to implement your own thread-safe counter alongside the mutex; off the top of my head I'm not sure that you could do that reliably either, but you could quite easily distinguish between "approximately none", "a few" and "lots" if that's good enough for your needs.
您可以在互斥锁请求周围放置一个互斥锁,该互斥锁请求会递增计数器,并且仅允许您在该计数器为零时访问互斥锁。
不过,这似乎是可怕的矫枉过正。
可能更好的方法是构建一个小队列并将所有请求添加到队列中(在push_back和pop_front周围有一个互斥体以及一个附加的peek_front)。
不过,这似乎是一个相当有趣的练习。
You could put a mutex around the mutex request that increments a counter and only allows you to access the mutex when that counter is zero.
This seems like terrible overkill, though.
Probably a better way to do this would be to build a small queue and have all requests added to the queue (with a mutex around the push_back and pop_front and an additional peek_front).
This seems a rather interesting exercise to do, though.