Synchronise Shared Memory in a multi threaded environment
I have implemented a Observer Pattern, in C++ project.
- My Subject is a XML File reader which reads tags and publishes its value.
- I have some "processing objects" which are my observers. They check 开发者_开发技巧the tag that has currently been read, if they have subsribed to the tag they will process it else ignore it.
- I have banks of memory into which the tags and their values are dumped into.
My problem no is, how do I synchronise the memory operations? When my XML reader wants to publish some tag/value, its should get a unused block of memory and "lock" it so that on its unavailable for editing. Once all the "Processing Objects" are done with the memory, they should be able to "unlock" for further use.
How can I achieve this? please help.
have you checked out boost shared memory? It has various synchronization mechanisms and examples...
The synchronization mechanisms outlined in the interprocess library are specifically useful if you want to put the mutexes in the shared memory block itself.
I assume your main task is not to learn/develop the synchronization mechanism.
You should reuse existing components available, and there are many. This is very good http://www.rabbitmq.com/getstarted.html
It will support multiple (including distributed/networking) models. Although there might initial learning period, but once integrated you can continue using it for your feature extensions, and focus on the meat of problem instead.
精彩评论