简体   繁体   English

是否可以在System V共享内存中实现无锁循环队列(C++写,Python读)

[英]Is it possible to implement a lock free circular queue in System V shared memory (write by C++, read by Python)

A lock free circular queue (single-reader single-writer) depends on atomic writes and a consistent view of the data for reads.无锁循环队列(单读取器单写入器)依赖于原子写入和读取数据的一致视图。

In it's simplest form you have a HEAD pointer, updated by the writer-process , and a sufficiently large array.最简单的形式中,您有一个HEAD指针,由writer-process更新,以及一个足够大的数组。 The writer-process writes new data, then updates the HEAD pointer. writer-process写入新数据,然后更新HEAD指针。

Presume the HEAD pointer is a 32 bit integer.假设HEAD指针是一个 32 位整数。 C offers atomic write operations on that 32 bit integer. C 提供对该 32 位整数的原子写操作。 But if the reader-process is Python ( multiprocessing.shared_memory + numpy ), it's not at all obvious that Python can assume a consistent view of that 4-byte HEAD pointer, or even any guarantee that the HEAD write operation will occur after the data is written to the array.但是如果读取器进程是 Python ( multiprocessing.shared_memory + numpy ),那么 Python 可以假设该 4 字节HEAD指针的一致视图,甚至任何保证HEAD写入操作将在数据之后发生的情况都不是很明显被写入数组。

Are there convenient ways to handle this between processes and languages?是否有方便的方法来处理进程和语言之间的问题?

You're mixing C and C++ here as well, but that's somewhat acceptable because they share a memory model.您也在这里混合使用 C 和 C++,但这在某种程度上是可以接受的,因为它们共享一个内存模型。

The convenient way would be a CPython extension in C, using the same compiler as the other side of the shared memory.方便的方法是在 C 中使用 CPython 扩展,使用与共享内存另一侧相同的编译器。

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

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