在无锁单链表的开头插入节点时,要使用的正确内存顺序是什么?

时间:2019-07-14 18:06:16

标签: c++ c++17 lock-free memory-barriers stdatomic

我有一个简单的链表。没有ABA问题的危险,我对阻塞类别感到满意,而且我不在乎我的列表是FIFO,LIFO还是随机的。只要插入成功而不使其他人失败。

该代码看起来像这样:

class Class {
  std::atomic<Node*> m_list;
  ...
};

void Class::add(Node* node)
{
  node->next = m_list.load(std::memory_order_acquire);
  while (!m_list.compare_exchange_weak(node->next, node, std::memory_order_acq_rel, std::memory_order_acquire));
}

我或多或少地随机填写了所使用的memory_order的。 在这里使用正确的存储顺序是什么?

我已经看到人们在所有地方都使用std::memory_order_relaxed,SO上的一个人也使用了std::memory_order_release,但是在compare_exchange_weak成功的案例中,然后是sudo apt-get update sudo apt-get install mysql-server sudo ufw allow mysql systemctl start mysql sudo /usr/bin/mysql -u root -p use mysql; update user set authentication_string=password('set_new_password') where user='root'; sudo mysql -u root -p enter your new password -genmc项目使用memory_order_acquire /两次memory_order_acq_rel在可比的情况下,但我无法让genmc用于测试用例:(。

1 个答案:

答案 0 :(得分:1)

使用Michalis Kokologiannakis genmc excellent 工具,我可以使用以下测试代码来验证所需的内存顺序。不幸的是,genmc当前需要C代码,但这与确定内存顺序当然无关紧要。

// Install https://github.com/MPI-SWS/genmc
//
// Then test with:
//
// genmc -unroll 5 -- genmc_sll_test.c

// These header files are replaced by genmc (see /usr/local/include/genmc):
#include <pthread.h>
#include <stdlib.h>
#include <stddef.h>
#include <assert.h>
#include <stdatomic.h>
#include <stdio.h>

#define PRODUCER_THREADS 3
#define CONSUMER_THREADS 2

struct Node
{
  struct Node* next;
};

struct Node* const deleted = (struct Node*)0xd31373d;

_Atomic(struct Node*) list;

void* producer_thread(void* node_)
{
  struct Node* node = (struct Node*)node_;

  // Insert node at beginning of the list.
  node->next = atomic_load_explicit(&list, memory_order_relaxed);
  while (!atomic_compare_exchange_weak_explicit(&list, &node->next,
             node, memory_order_release, memory_order_relaxed))
    ;

  return NULL;
}

void* consumer_thread(void* param)
{
  // Replace the whole list with an empty list.
  struct Node* head = atomic_exchange_explicit(&list, NULL, memory_order_acquire);
  // Delete each node that was in the list.
  while (head)
  {
    struct Node* orphan = head;
    head = orphan->next;
    // Mark the node as deleted.
    assert(orphan->next != deleted);
    orphan->next = deleted;
  }

  return NULL;
}

pthread_t t[PRODUCER_THREADS + CONSUMER_THREADS];
struct Node n[PRODUCER_THREADS]; // Initially filled with zeroes -->
                                 // none of the Node's is marked as deleted.

int main()
{
  // Start PRODUCER_THREADS threads that each append one node to the queue.
  for (int i = 0; i < PRODUCER_THREADS; ++i)
    if (pthread_create(&t[i], NULL, producer_thread, &n[i]))
      abort();

  // Start CONSUMER_THREAD threads that each delete all nodes that were added so far.
  for (int i = 0; i < CONSUMER_THREADS; ++i)
    if (pthread_create(&t[PRODUCER_THREADS + i], NULL, consumer_thread, NULL))
      abort();

  // Wait till all threads finished.
  for (int i = 0; i < PRODUCER_THREADS + CONSUMER_THREADS; ++i)
    if (pthread_join(t[i], NULL))
      abort();

  // Count number of elements still in the list.
  struct Node* l = list;
  int count = 0;
  while (l)
  {
    ++count;
    l = l->next;
  }

  // Count the number of deleted elements.
  int del_count = 0;
  for (int i = 0; i < PRODUCER_THREADS; ++i)
    if (n[i].next == deleted)
      ++del_count;

  assert(count + del_count == PRODUCER_THREADS);
  //printf("count = %d; deleted = %d\n", count, del_count);

  return 0;
}

其输出为

  

$ genmc -unroll 5-genmc_sll_test.c
  探索的完整执行次数:6384
  总挂钟时间:1.26s

memory_order_releasememory_order_acquire替换为memory_order_relaxed会引起断言。

实际上,可以检查出,仅插入节点时使用排他memory_order_relaxed就足以将它们清楚地显示在列表中(尽管以“随机”顺序排列-没有顺序一致,因此顺序如果由于其他原因而存在这种关联,则添加它们的方式与线程尝试添加它们的方式不一定相同。

但是,memory_order_release是必需的,因此当用head读取memory_order_acquire时,我们可以确定所有非原子的next指针在“消费者”中都是可见的“线程。

请注意,这里没有ABA问题,因为用于headnext的值在被'consumer_thread'函数删除之前是不能“重用”的,这是这些节点所在的唯一位置(因此)允许删除,这意味着只能有一个使用者线程(此测试代码不会检查ABA问题,因此它也可以使用2 CONSUMER_THREADS起作用)。

实际代码是一种垃圾回收机制,其中多个“生产者”线程可以删除时将它们添加到指向单链接列表的指针,但是实际上只有在一个特定线程中这样做才是安全的(在这种情况下,因此只有一个“消费者”线程,该线程在主循环中一个众所周知的位置执行此垃圾回收。