mardi 27 juin 2017

C++11 multi producer single consumer queue

I'm calculating values on N threads at variable rates between 100-1000/sec per thread. I'd like to pass these on to a consumer that will produce stats about once a second. The idea is that the process of communicating the values out of the producer should have as little contention as possible (ideally under a microsecond or even 500 nanos).

I've looked at single producer single consumer implementations and they are fairly straightforward to implement using std::atomic's. My first attempt at this was to use a std::atomic to hold the writing position and use fetch_add(1) which seems to work nicely on the producer side. But the problem is that the fetch_add happens before the write to the circular buffer so there's a possibility that the reader thread might not get the last few values written.

I could illustrate with code if you want, but I think my description is fairly straightforward.

Another solution would be to use 1 SPSC queue per producer thread but then that would incur a bit more management for the consumer to gather everything up.

Any ideas on a good design for this problem?

Aucun commentaire:

Enregistrer un commentaire