boost中有没有一个函数可以查找有多少人正在等待互斥锁?

发布于 2024-11-30 23:39:35 字数 49 浏览 0 评论 0原文

我使用的是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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

孤独难免 2024-12-07 23:39:35

否:没有办法获得可靠的答案,因为它可能在函数返回之前发生变化,并且 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.

对不⑦ 2024-12-07 23:39:35

您可以在互斥锁请求周围放置一个互斥锁,该互斥锁请求会递增计数器,并且仅允许您在该计数器为零时访问互斥锁。

不过,这似乎是可怕的矫枉过正。

可能更好的方法是构建一个小队列并将所有请求添加到队列中(在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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文