第十七课:C++多线程的无锁线程安全链表

/*
    无锁线程安全链表

    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
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值