Lock free synchronization

后端 未结 3 666
盖世英雄少女心
盖世英雄少女心 2021-02-15 16:55

My question is related to multithreading lock-free synchronization. I wanted to know the following:

  1. What are general approaches to achieve this? I read somewher

3条回答
  •  一个人的身影
    2021-02-15 17:03

    There are some useful way to use lock-free sychronization (such as those @Tudor mentions). But I want to warn about one thing - lock-free syncrhonization doesn't compose.

    You may have, for example, an integer maintained by compare&swap, and it's OK. You may also have a queue, maintained by a lock-free algorithms (it's a bit tricky, but there are good algorithms for it), and the queue is also OK.
    But if you try to use the counter to count the elements in a queue, you'll get wrong answers. There will be times when an element was added, but the counter doesn't yet reflect it (or vice versa), and you can get bugs if you trust it (e.g. you may try to add to a full queue).

    In short - you can have each element consistent with itself, but not consistent with each other.

提交回复
热议问题