Thread-safe C++ stack

后端 未结 6 1414
余生分开走
余生分开走 2021-01-30 18:56

I\'m new to C++ and am writing a multi-threaded app whereby different writers will be pushing objects onto a stack and readers pulling them off the stack (or at least pushing th

6条回答
  •  無奈伤痛
    2021-01-30 19:29

    If you don't want to use locking, you need to use a lock-free stack. This is actually not that hard (lock-free queue is more difficult). You do need a platform-specific compare-exchange primitive, such as InterlockedCompareExchange on Windows, but this isn't hard to abstract.

    See here for an example in C#:

    http://www.boyet.com/Articles/LockFreeRedux.html

提交回复
热议问题