C++/CLI efficient multithreaded circular buffer
I have four threads in a C++/CLI GUI I'm developing:
- Collects raw data
- The GUI itself
- A background processing thread which takes chunks of raw data and produces useful information
- Acts as a controller which joins the other three threads
I've got the raw data collector working and posting results to the controller, but the next step is to store all of those results so that the GUI and background processor have access to them.
New raw data is fed in one result at a time at regular (frequent) intervals. The GUI will access each new item as it arrives (the controller announces new data and the GUI then accesses the shared buffer). The data processor will periodically read a chunk of the buffer (a seconds worth for example) and produce a new result. So effectively, there's one producer and two consumers which need access.
I'v开发者_运维百科e hunted around, but none of the CLI-supplied stuff sounds all that useful, so I'm considering rolling my own. A shared circular buffer which allows write-locks for the collector and read locks for the gui and data processor. This will allow multiple threads to read the data as long as those sections of the buffer are not being written to.
So my question is: Are there any simple solutions in the .net libraries which could achieve this? Am I mad for considering rolling my own? Is there a better way of doing this?
Is it possible to rephrase the problem so that:
- The Collector collects a new data point ...
- ... which it passes to the Controller.
- The Controller fires a GUI "NewDataPointEvent" ...
- ... and stores the data point in an array.
- If the array is full (or otherwise ready for processing), the Controller sends the array to the Processor ...
- ... and starts a new array.
If the values passed between threads are not modified after they are shared, this might save you from needing the custom thread-safe collection class, and reduce the amount of locking required.
精彩评论