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

发布于 2024-11-28 15:22:23 字数 209 浏览 1 评论 0原文

我有一块共享内存,其中包含两个进程之间的一个字符串和一个整数。

进程 A 写入它,进程 B 读取它(反之亦然)

什么是最有效和有效的方法来确保进程 A 不会在进程 B 读取它的同时更新(写入它)? (我应该在共享内存中使用标志,使用信号量,临界区......)

如果你能指出我正确的方向,我将不胜感激。

谢谢。

视窗、C++

I have a piece of shared memory that contains a char string and an integer between two processes.

Process A writes to it and Process B reads it (and not vice versa)

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? (Should I just use flags in the shared memory, use semaphores, critical section....)

If you could point me in the right direction, I would appreciate it.

Thanks.

Windows, C++

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

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

发布评论

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

评论(4

山川志 2024-12-05 15:22:23

您不能使用关键部分,因为它们可以只用于同一进程内线程之间的同步。对于进程间同步,您需要使用 互斥体< /a> 或 信号量。两者之间的区别在于,前者仅允许单个线程拥有资源,而后者可以允许最多数量(在创建期间指定)同时拥有资源。

在你的情况下,互斥体似乎是合适的。

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

薯片软お妹 2024-12-05 15:22:23

由于您有两个进程,因此需要一个跨进程同步对象。我认为这意味着您需要使用 互斥体

Since you have two processes you need a cross-process synchronisation object. I think this means that you need to use a mutex.

银河中√捞星星 2024-12-05 15:22:23

互斥对象有助于防止数据竞争并允许
线程之间数据的线程安全同步。一个线程获取
通过调用锁定函数之一来获得互斥对象的所有权,并且
通过调用相应的解锁函数放弃所有权。

如果您使用boost线程,您可以使用它的互斥锁和锁定,更多内容请参阅下面的链接:

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

A mutex object facilitates protection against data races and allows
thread-safe synchronization of data between threads. A thread obtains
ownership of a mutex object by calling one of the lock functions and
relinquishes ownership by calling the corresponding unlock function.

If you are using boost thread, you can use it's mutex and locking, more to read see the link below:

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

无人问我粥可暖 2024-12-05 15:22:23

由于您谈论的是两个进程,因此系统范围的互斥体将起作用,并且 Windows 具有这些互斥体。然而,它们不一定是最有效的方法。

如果您可以将更多内容放入共享内存中,那么通过对该内存中的标志进行原子操作来传递数据应该是最有效的做法。例如,您可以使用互锁函数 实施 Dekker 的算法(您可能需要使用类似 YieldProcessor() 以避免忙等待)。

Since you're talking about two processes, system-wide mutexes will work, and Windows has those. 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. 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).

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