简体   繁体   English

锁定两个进程而不是线程的自由/原子操作

[英]lock freedom/atomic operations across 2 processes instead of threads

I am sharing some data across multiple processes by using shared memory; 我通过使用共享内存在多​​个进程之间共享一些数据; I use inter processes mutexes to achieve synchronization. 我使用进程间互斥来实现同步。

My question is the following: is it possible to use lock-free data structures AND/OR atomic operations to achieve faster synchronization without using mutexes between 2 processes? 我的问题如下:是否可以使用无锁数据结构和/或原子操作来实现更快的同步,而无需在两个进程之间使用互斥锁?

If not do you know what is the main reason for this? 如果不是,你知道这是什么主要原因?

They are used only to synchronize threads of the same process. 它们仅用于同步同一进程的线程。 Are these concepts portable to processes as well? 这些概念是否也可以移植到流程中? If they aren't do you know any faster method to share/synchronize data across processes? 如果不是,您是否知道跨进程共享/同步数据的更快方法?

Are these concepts portable to processes as well? 这些概念是否也可以移植到流程中?

Yes, atomic operations are universal both for threads and processes, IIF the memory atomically used is shared. 是的,原子操作对于线程和进程都是通用的,IIF原子使用的内存是共享的。

Atomic operation is specific instruction of processor itself and in knows nothing about threads or processes, it is just All-or-nothing (indivisible) complex of actions (read; compare; store) with low-level hardware implementation. 原子操作是处理器本身的特定指令,并且对线程或进程一无所知,它只是动作(读取;比较;存储)的全有或全无(不可分割)复杂与低级硬件实现。

So, you can setup shared memory between processes and put an atomic_t into it. 因此,您可以在进程之间设置共享内存并将atomic_t放入其中。

lock-free 无锁

Yes, if lock-free is implemented only with atomic. 是的,如果只使用原子实现无锁。 (It should) (这应该)

data structures 数据结构

You should check, that shared memory is mapped to the same address in both processes when it is used to store pointers (in data structures). 您应该检查,当共享内存用于存储指针时(在数据结构中),共享内存将映射到两个进程中的相同地址。

If the memory will be mapped to different address, pointers will be broken in another process. 如果内存将映射到不同的地址,则指针将在另一个进程中被中断。 In this case you need to use relative addresses, and do simple memory translation. 在这种情况下,您需要使用相对地址,并进行简单的内存转换。

inter processes mutexes 进程间互斥

And I should say that glibc>2.4 (NPTL) uses futex combined with atomic operations for non-contended lock (for Process shared mutexes = inter process mutexes). 我应该说glibc> 2.4(NPTL)使用futex结合原子操作进行非竞争锁定(对于Process共享互斥锁=进程间互斥锁)。 So, you already use atomic operations in shared memory. 因此,您已经在共享内存中使用原子操作。

On x86 with NPTL, most of the synchronization primitives have as their fast path just a single interlocked operation with a full memory barrier. 在带有NPTL的x86上,大多数同步原语的快速路径只有一个带有完整内存屏障的互锁操作。 Since x86 platforms don't really have anything lighter than that, they are already about the best you can do. 由于x86平台没有比这更轻的东西,它们已经是你能做的最好的了。 Unless the existing atomic operations do exactly what you need to do, there will be no performance boost to pay back the costs of using the semantically lighter primitive. 除非现有的原子操作完全按照您的需要进行操作,否则将无法提高性能,以降低使用语义较轻的原语的成本。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM