Circular lock-free buffer

后端 未结 18 720
悲哀的现实
悲哀的现实 2020-11-29 15:00

I\'m in the process of designing a system which connects to one or more stream of data feeds and do some analysis on the data than trigger events based on the result. In a t

18条回答
  •  情歌与酒
    2020-11-29 15:40

    One useful technique to reduce contention is to hash the items into multiple queues and have each consumer dedicated to a "topic".

    For most-recent number of items your consumers are interested in - you don't want to lock the whole queue and iterate over it to find an item to override - just publish items in N-tuples, i.e. all N recent items. Bonus points for implementation where producer would block on the full queue (when consumers can't keep up) with a timeout, updating its local tuple cache - that way you don't put back-pressure on the data source.

提交回复
热议问题