Thread safe implementation of circular buffer

后端 未结 5 771
攒了一身酷
攒了一身酷 2020-11-29 02:25

Circular_buffer from boost library is not thread-safe. So I wrapped boost::circular_buffer object in a class as shown below. Mutual exclusion between the threads is achieved

相关标签:
5条回答
  • 2020-11-29 02:46

    i think it looks fine, except that there is some pointless copies of Mat made in send. You don't need the new, you can directly push the argument of send to your cb.

    0 讨论(0)
  • 2020-11-29 02:47

    Your implementation is similar to the one shown by this blogger. You should read that blog to see if you missed anything in your implementation.

    If your Mat objects are expensive to create/copy, you should avoid continuously creating/copying/deleting them. Instead, you should have a pool (aka free list) of Mat objects that continually get recycled in some kind of pipeline architecture. I describe this type of architecture in this answer to a related question.

    In that answer, I suggested using a blocking stack to implement the pool, but you could also use your blocking circular_buffer. The reason I suggested a stack was because I thought it may be more cache-friendly, but I never actually measured to see if it would make a difference.

    0 讨论(0)
  • 2020-11-29 02:47

    Very old question :) Here is a dis gin with lock-free implementation Link

    Here is a BSD-2 Lib for this Link

    0 讨论(0)
  • 2020-11-29 02:53

    Yes.
    If you lock all the public methods with the same lock it will be threadsafe.

    You could consider using read-write locks, which may have better performance if you have a lot of concurrent readers.

    If you don't have a lot of readers, it will just add overhead, but may be worth checking the option and testing.

    0 讨论(0)
  • 2020-11-29 03:09

    Looks good at first glance, except that you are not using the buffer_not_full condition at all. You probably want to add code similar to the buffer_not_empty code.

    0 讨论(0)
提交回复
热议问题