Any single-consumer single-producer lock free queue implementation in C?

后端 未结 9 1198
悲哀的现实
悲哀的现实 2020-12-24 04:03

I\'m writing a program with a consumer thread and a producer thread, now it seems queue synchronization is a big overhead in the program, and I looked for some lock free que

相关标签:
9条回答
  • 2020-12-24 04:27

    You should look at FastFlow library

    0 讨论(0)
  • 2020-12-24 04:37

    If you are worried about performance, adding malloc() to the mix won't help things. And if you are not worried about performance, why not simply control access to the queue via a mutex. Have you actually measured the performance of such an implementation? It sounds to me as though you are going down the familar route of premature optimisation.

    0 讨论(0)
  • 2020-12-24 04:37

    I think the allocator can be a performance problem. You can try to use a custom multithreaded memory allocator, that use a linked-list for maintaing freed blocks. If your blocks are not (nearly) the same size, you can implement a "Buddy system memory allocator", witch is very fast. You have to synchronise your queue (ring buffer) with a mutex.

    To avoid too much synchronisation, you can try write/read multiple values to/from the queue at each access.

    If you still want to use, lock-free algorithms, then you must use pre-allocated data or use a lock-free allocator. There is a paper about a lock-free allocator "Scalable Lock-Free Dynamic Memory Allocation", and an implementation Streamflow

    Before starting with Lock-free stuff, look at:Circular lock-free buffer

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