Optimize Concurrent writes to a buffer
I am required to have multiple threads writing to a single buffer (contiguous chunk of memory). The brute force method will be as follow
- The thread that wants to write to buffer will acquire lock on the buffer
- Entire buffer is locked and therefore only the thread that acquired lock can modify the buffer.
- The thread write to buffer.
- The thread unlocks the buffer.
This method serializes all threads because only one thread is active at given time. This turns out to be a bottleneck as the application spends majority of time 开发者_StackOverflow中文版writing to the buffer.
Could someone please suggest a method to increase parallelism while writing to single buffer?
Many thanks in advance.
Have your threads write their data to a queue instead. Then, let a dedicated thread write from the queue to the buffer. If that is not concurrent enough, sacrifice the fixed ordering and use multiple queues.
If possible, you can let the threads write to different locations of the buffer with different size such that no two threads writing to the same location concurrently and acquire lock accordingly.
Herb Sutter posted a very good article just a few days ago describing very nearly this exact situation. He discusses using an active object
to handle the concurrency problem of writing to the shared buffer. You can view the article by going to his web site and following the link or this link may work.
精彩评论