代码之家  ›  专栏  ›  技术社区  ›  dutt

同步锁的替代方案

  •  12
  • dutt  · 技术社区  · 14 年前

    我目前正在开发自己的小线程库,主要是为了学习目的,并且处于消息队列的一部分,这将在不同的地方涉及大量的同步。以前我主要使用锁、互斥锁和条件变量,它们都是同一主题的变体,这是一个部分的锁,一次只能由一个线程使用。

    同步有没有不同于使用锁的解决方案?我在某些地方读过无锁同步,但有些人考虑将容器中的锁隐藏为无锁,这一点我不同意。你不需要自己明确地使用锁。

    4 回复  |  直到 7 年前
        1
  •  13
  •   Tony Delroy    7 年前

    1 do
    2 {
    3     current_value = the_varibale
    4     new_value = ...some expression using current_value...
    5 } while(!compare_and_swap(the_variable, current_value, new_value));
    
    • compare_and_swap() the_variable current_value new_value true

        4
  •  1
  •   Matthieu M.    14 年前