views:

59

answers:

3

Hi,

I am required to have multiple threads writing to a single buffer (contiguous chunk of memory). The brute force method will be as follow

  1. The thread that wants to write to buffer will acquire lock on the buffer
  2. Entire buffer is locked and therefore only the thread that acquired lock can modify the buffer.
  3. The thread write to buffer.
  4. 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 writing to the buffer.

Could someone please suggest a method to increase parallelism while writing to single buffer?

Many thanks in advance.

+4  A: 

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.

Peter G.
+1, 30 seconds faster than me;
arthurprs
@Peter : I think in your solution the dedicated thread will be the bottleneck. Looks this solution postpones the problem for a little while but eventually we may end up in same situation.
LionHeart
The buffer write thread has exclusive write access to the buffer, so no bottleneck here. The queues can be concurrently read and written, no bottleneck here.
Peter G.
@LionHeart: simple FIFO queues can be implemented lockless, which make them very fast, you can probably find such implementation around, I know Sutter dedicated a whole blog post to such thing (using a circular buffer for the queue). Therefore the dedicated thread will never lock when retrieving from the queue nor will it lock when writing to the buffer. There is still the cost of writing, obviously.
Matthieu M.
A: 

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.

Donotalo
A: 

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.

Mark Wilkins