本文介绍了是boost :: interprocess threadsafe吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我有2个进程使用message_queue和shared_memory进行通信。

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

现在我需要做一个这个过程多线程(再次感谢boost),我想知道我是否需要使用保护机制线程(如mutex),或者如果boost :: interprocess库已经提供了保护机制?

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 ?

我没有找到关于boost文档的任何信息。我正在使用boost 1.40。

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

提前感谢。

推荐答案

boost :: interprocess(共享内存等)的共享资源需要您提供必要的同步。这样做的原因是你可能不需要同步,并且它通常是一个有点贵的操作性能明智。

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.

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

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.

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

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]

[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]

[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.

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

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.

这篇关于是boost :: interprocess threadsafe吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-03 21:30
查看更多