I use C# (MS visual studio 2010) to simulate the Producer-Consumer problem. I have 2 exe programs, one is Producer and another is Consumer. When they're executing, Producer will insert 100 integers into a Queue, while Consumer will remove them from that Queue in orderly way.
My problem is how to use a shared Queue between two processes, and in case I have to use inter-process communication to pass some small messages, which mechanism should I use (in Clipboard, COM, Data Copy, DDE, File Mapping, Mailslots, Pipes, RPC, Windows Sockets)?
Thank you very mu开发者_开发百科ch!
I would simply use a message queuing technology such as MSMQ. Look into this to see if it would suit your requirements. You could use this alongside NServiceBus which gives you the full pub/sub model off the shelf and is relatively easy to use.
Another option I would think of using a database transaction table with concurrency control on the table between multiple read/writes at the same time.
It would be far easier if the two processes were converted into threads within the same process, so they could directly share memory.
Barring that, you could use Memory-mapped I/O to establish a region of shared memory between the processes and a named mutex to ensure that only one thread accesses it as a time. You would have to write your own queue over a raw pointer, which is easy to get wrong.
It's possible for a queue to be lockless, even in this case, but that's much harder to pull off, and probably not worth it.
精彩评论