rigtorp / MPMCQueue

A bounded multi-producer multi-consumer concurrent queue written in C++11
MIT License
1.15k stars 159 forks source link

Does this work on shared memory? #16

Closed Jerry-Ma closed 4 years ago

Jerry-Ma commented 4 years ago

For example, Linux open_shm, or MPI_win_allocate_shared, combined with placement new:

const int N = 10; // capacity
std::byte* buffer;
/* allocate buffer as shm on main proc*/
if (/*main proc*/) {
    new (buffer) MPMCQueue<int> q(N);
    // consume
} else {
    new (buffer) MPMCQueue<int> q(N);
   // produce
}
rigtorp commented 4 years ago

Not out of the box, but the changes needed to make it work are small. The constructor calls malloc to allocate memory for the data. You need to replace that with a call to allocate shared memory. I have been thinking about adding support for custom allocators.

On Wed, Oct 16, 2019 at 3:57 PM Jerry-Ma notifications@github.com wrote:

For example, Linux open_shm, or MPI_win_allocate_shared, combined with placement new:

const int N = 10; // capacity std::byte buffer / allocate buffer as shm on main proc/ if (/main proc*/) { new (&buffer) MPMCQueue q(N) // consume } else { new (&buffer) MPMCQueue<int> q(N) // produce }

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/rigtorp/MPMCQueue/issues/16?email_source=notifications&email_token=AABLO26VL3TGZLVWQRCZ3YTQO6L4RA5CNFSM4JBSAIY2YY3PNVWWK3TUL52HS4DFUVEXG43VMWVGG33NNVSW45C7NFSM4HSJUEQA, or unsubscribe https://github.com/notifications/unsubscribe-auth/AABLO2Z66YQX3BCW33LSLADQO6L4RANCNFSM4JBSAIYQ .

Jerry-Ma commented 4 years ago

I am quite new to the concept of lock free, so the use of atomic operations behind the scene will just work for shared memory pointers, right?

I know that for example std::mutex does not work in IPC context, but boost::interprocess::mutex works. This is why I had this question.

rigtorp commented 4 years ago

Not necessarily. It's only guaranteed for main memory, not mapped device memory. If you are using non temporal reads and stores you might also need to additional code to deal with that.

This queue algorithm will work with mmaped main memory.

On Wed, Oct 16, 2019, 16:06 Jerry-Ma notifications@github.com wrote:

I am quite new to the concept of lock free, so the use of atomic operations behind the scene will just work for shared memory pointers, right?

I know that for example std::mutex does not work in IPC context, but boost::interprocess::mutex works. This is why I had this question.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/rigtorp/MPMCQueue/issues/16?email_source=notifications&email_token=AABLO252L366Q2TSFFCFT5TQO6M7LA5CNFSM4JBSAIY2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEBOGR4I#issuecomment-542927089, or unsubscribe https://github.com/notifications/unsubscribe-auth/AABLO22NKXKJJZCMS4TWG6LQO6M7LANCNFSM4JBSAIYQ .

rigtorp commented 4 years ago

@Jerry-Ma Latest version of MPMCQueue now supports custom allocators. You should be able to use a custom allocator in order to use shared memory.