Boost 的范围互斥体和 WinAPi 的临界区之间有区别吗?

发布于 2024-07-19 09:07:06 字数 51 浏览 3 评论 0原文

在Windows环境中,Boost的作用域互斥体是使用WinAPI的临界区还是其他什么?

In Windows environment, is Boost's scoped mutex using WinAPI's critical sections, or something else?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

南巷近海 2024-07-26 09:07:06

当前版本的 boost::mutex 既不使用 Win32 CRITICAL_SECTION,也不使用 Win32 Mutex。 相反,它使用原子操作和 Win32 事件来阻塞等待。

旧版本(boost 1.34.1 及之前版本)是 Windows 上 CRITICAL_SECTION 的包装。

顺便说一句,互斥锁本身没有作用域。 boost::mutex::scoped_lock 类型以及最近版本中的 boost::lock_guardboost::unique_lock 提供用于锁定互斥体的 RAII 包装器,以确保您不会忘记解锁它。

boost::lock_guard<>boost::unique_lock<> 模板适用于具有 lock()的任何类型unlock() 成员函数,因此如果需要,您可以将它们与进程间互斥体一起使用。

The current version of boost::mutex uses neither a Win32 CRITICAL_SECTION, nor a Win32 Mutex. Instead, it uses atomic operations and a Win32 Event for blocking waits.

Older versions (boost 1.34.1 and prior) were a wrapper around CRITICAL_SECTION on Windows.

Incidentally, the mutex itself is not scoped. The boost::mutex::scoped_lock type and, in recent versions, boost::lock_guard<boost::mutex> and boost::unique_lock<boost::mutex> provide RAII wrappers for locking a mutex to ensure you don't forget to unlock it.

The boost::lock_guard<> and boost::unique_lock<> templates work with any type with lock() and unlock() member functions, so you can use them with inter-process mutexes if desired.

眼眸里的快感 2024-07-26 09:07:06

Win32 的 CRITICAL_SECTION 只能在单个进程的线程中使用。 如果需要在进程之间使用某些东西,则需要互斥体。 Boost 没有提及关键部分,因此我假设它正在使用互斥体。

“scoped”只是意味着它有一个包装器,使用 RAII 在最后自动解锁互斥体属于特定范围。

Win32's CRITICAL_SECTION can only be used among the threads of a single process. If you need to use something between processes, you need a mutex. Boost says nothing about critical sections so I would assume it is using mutexes.

"scoped" just means it has a wrapper that uses RAII to automatically unlock the mutex at the end of a particular scope.

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