boost::进程间线程安全吗?

发布于 2025-01-04 19:08:01 字数 233 浏览 3 评论 0原文

目前,我有 2 个进程使用 message_queue 和共享内存形式 boost 进行通信。一切都按参加的方式进行。

现在我需要使这个进程之一成为多线程(再次感谢boost),我想知道是否需要在线程之间使用保护机制(例如互斥体),或者boost::interprocess库是否已经提供了保护机制 ?

我在 boost 文档中没有找到任何相关信息。顺便说一句,我用的是boost 1.40。

提前致谢。

Currently, I have 2 processes that communicate using the message_queue and shared_memory form boost. Everything work as attended.

Now I need to make one of this process multi threaded (thanks to boost again), and I was wondering if I need to use protection mechanism between the threads (such as mutex), or if the boost::interprocess library already provides a protection mechanism ?

I did not find any info on that on the boost documentation. By the way I'm using boost 1.40.

Thanks in advance.

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

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

发布评论

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

评论(2

梦情居士 2025-01-11 19:08:01

来自 boost::interprocess 的共享资源(共享内存等)要求您提供必要的同步。这样做的原因是您可能不需要同步,而且这通常是一种有点昂贵的操作性能。

举例来说,您有一个进程以 32 位整数格式将某些内容的当前统计信息写入共享内存,还有一些进程读取这些值。由于这些值是整数(因此在您的平台上,读取和写入是原子的)并且您有一个写入它们的进程和几个读取它们的进程,因此此设计不需要同步。

然而,在某些示例中,您将需要同步,例如上面的示例有多个编写器,或者如果您使用字符串数据而不是整数。 boost 内部有各种同步机制(以及非 boost 同步机制,但由于您已经使用 boost),如下所述:

[稳定版本的 Boost 信息:1.48]
http://www.boost.org/doc/libs /1_48_0/doc/html/interprocess/synchronization_mechanisms.html

[您使用的版本的增强信息:1.40]
http://www.boost.org/doc/libs /1_40_0/doc/html/interprocess/synchronization_mechanisms.html

对于共享内存,通常的做法是将同步机制放置在共享内存段的底部,这样它就可以是匿名的(意味着操作系统内核不提供按名称对其进行访问)。这样,所有进程都知道如何锁定共享内存段,并且您可以将锁与其段相关联(例如,如果您有多个段)

请记住,互斥体需要相同的执行线程(在进程内)来解锁已锁定的它它。如果需要从不同的执行线程锁定和解锁同步对象,则需要信号量。

请确保,如果您选择使用互斥锁,它是进程间互斥锁 (http://www.boost.org/doc/libs/1_48_0/doc/html/boost/interprocess/interprocess_mutex.html),而不是boost线程库中的互斥体适用于具有多个线程的单个进程。

The shared resources from boost::interprocess (shared memory, etc) require that you provide the necessary synchronization. The reason for this is that you may not require synchronization, and it usually a somewhat expensive operation performance wise.

Say for example that you had a process that wrote to shared memory the current statistics of something in 32-bit integer format, and a few processes that read those values. Since the values are integers (and therefore on your platform the reads and writes are atomic) and you have a single process writing them and a few process reading them, no synchronization is needed for this design.

However in some examples you will require synchronization, like if the above example had multiple writers, or if instead of integers you were using string data. There are various synchronization mechanisms inside of boost (as well as non-boost ones, but since your already using boost), described here:

[Boost info for stable version: 1.48]
http://www.boost.org/doc/libs/1_48_0/doc/html/interprocess/synchronization_mechanisms.html

[Boost info for version your using: 1.40]
http://www.boost.org/doc/libs/1_40_0/doc/html/interprocess/synchronization_mechanisms.html

With shared memory it is a common practice to place the synchronization mechanism at the base of the shared memory segment, where it can be anonymous (meaning the OS kernel does not provide access to it by name). This way all the processes know how to lock the shared memory segment, and you can associate locks with their segments (if you had multiple for example)

Remember that a mutex requires the same thread of execution (inside a process) to unlock it that locked it. If you require locking and unlocking a synchronization object from different threads of execution, you need a semaphore.

Please be sure that if you choose to use a mutex that it is an interprocess mutex (http://www.boost.org/doc/libs/1_48_0/doc/html/boost/interprocess/interprocess_mutex.html) as opposed to the mutex in the boost thread library which is for a single process with multiple threads.

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