/*
无锁线程安全链表
compare_exchange_weak(oldValue,newValue);
当前值与期望值,相等时,修改当前值为设置值,返回true
当前值与期望值,不等时,讲期望值修改当前值,返回false
一般在循环中使用,不然就需要使用strong版本
*/
#include <thread>
#include <iostream>
#include <numeric>
#include <chrono>
#include <execution>
#include <algorithm>
#include <vector>
#include <random>
#include <cstdio>
using namespace std;
using namespace this_thread;
class threadsfae_list
{
protected:
struct node
{
int value;
node* next;
node(int date, node* next) :value(date), next(next) {}
};
atomic<node*>list_head;
public:
void push_front(int date)
{
node* oldHead = list_head;
node* newNode = new node(date, oldHead);
while (!list_head.compare_exchange_weak(oldHead, newNode))
{
newNode->next = oldHead;
}
}
void printf
第十七课:C++多线程的无锁线程安全链表
于 2023-09-27 10:31:50 首次发布