简体   繁体   English

如何保护两个进程之间共享 memory 中的字符串?

[英]How do I protect a character string in shared memory between two processes?

I have a piece of shared memory that contains a char string and an integer between two processes.我有一块共享的 memory 包含一个字符字符串和一个 integer 在两个进程之间。

Process A writes to it and Process B reads it (and not vice versa)进程 A 写入它,进程 B 读取它(反之亦然)

What is the most efficient and effective way to make sure that Process A doesn't happen to update (write to it) that same time Process B is reading it?确保进程 A 在进程 B 读取它的同时不会发生更新(写入)的最有效和最有效的方法是什么? (Should I just use flags in the shared memory, use semaphores, critical section....) (我是否应该只在共享 memory 中使用标志,使用信号量,临界区......)

If you could point me in the right direction, I would appreciate it.如果你能指出我正确的方向,我将不胜感激。

Thanks.谢谢。

Windows, C++ Windows, C++

You cannot use a Critical Section because these can only be used for synchronization between threads within the same process.您不能使用临界区,因为它们只能用于同一进程内的线程之间的同步。 For inter process synchronization you need to use a Mutex or a Semaphore .对于进程间同步,您需要使用MutexSemaphore The difference between these two is that the former allows only a single thread to own a resource, while the latter can allow up to a maximum number (specified during creation) to own the resource simultaneously.这两者的区别在于前者只允许单个线程拥有资源,而后者可以允许最大数量(在创建期间指定)同时拥有资源。

In your case a Mutex seems appropriate.在您的情况下,互斥锁似乎合适。

Since you have two processes you need a cross-process synchronisation object.由于您有两个进程,因此您需要跨进程同步 object。 I think this means that you need to use a mutex .我认为这意味着您需要使用mutex

A mutex object facilitates protection against data races and allows thread-safe synchronization of data between threads.互斥锁 object 有助于防止数据竞争,并允许线程之间的数据线程安全同步。 A thread obtains ownership of a mutex object by calling one of the lock functions and relinquishes ownership by calling the corresponding unlock function.线程通过调用其中一个锁定函数获得互斥体 object 的所有权,并通过调用相应的解锁 function 放弃所有权。

If you are using boost thread, you can use it's mutex and locking, more to read see the link below:如果您使用的是 boost 线程,则可以使用它的互斥锁和锁定,更多内容请参见下面的链接:

http://www.boost.org/doc/libs/1_47_0/doc/html/thread/synchronization.html#thread.synchronization.mutex_types http://www.boost.org/doc/libs/1_47_0/doc/html/thread/synchronization.html#thread.synchronization.mutex_types

Since you're talking about two processes, system-wide mutexes will work, and Windows has those.由于您在谈论两个进程,因此系统范围的互斥锁将起作用,并且 Windows 拥有这些。 However, they aren't necessarily the most efficient way.但是,它们不一定是最有效的方法。

If you can put more things in shared memory, then passing data via atomic operations on flags in that memory should be the most efficient thing to do.如果您可以在共享 memory 中放入更多内容,那么通过 memory 中的标志的原子操作传递数据应该是最有效的做法。 For instance, you might use the Interlocked functions to implement Dekker's Algorithm (you'll probably want to use something like YieldProcessor() to avoid busy waiting).例如,您可以使用Interlocked 函数来实现Dekker 算法(您可能希望使用YieldProcessor()类的东西来避免忙于等待)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在共享内存的同一区域上工作的两个进程之间共享锁? - How do I share locks between two processes working on the same region of shared memory? 进程之间不共享共享内存 - shared memory is not shared between processes 可以增加不同CPU上的进程之间的共享内存 - Can boost do shared memory between processes that are on different CPUs 我是否需要使用内存屏障来保护共享资源? - Do I need to use memory barriers to protect a shared resource? 如何使用运行时大小参数构造 boost spsc_queue 以使用共享内存在两个进程之间交换 cv::Mat 对象? - How to construct boost spsc_queue with runtime size parameter to exchange cv::Mat objects between two processes using shared memory? Ubuntu在两个进程之间共享内存,代码不起作用 - Ubuntu Shared Memory Between 2 Processes, Code Not Working C ++和Java进程之间的共享内存 - Shared memory between C++ and Java processes 在两个进程(C和C ++)之间提升Boost的managed_shared_memory用法 - Boost's managed_shared_memory usage in between two processes (C and C++) 尝试写入 memory 映射文件时出现问题,在两个进程之间共享(32 位 -> 64 位) - Issue when trying to write to memory mapped file, shared between two processes (32 bit -> 64 bit) 如何从boost共享内存向量中获取字符串,而不是从检索到的字符串的构造函数中触发共享内存中的分配? - How do I get a string out of a boost shared memory vector w/o triggering an allocation in shared memory from the constructor of the retrieved string?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM