C++ 如何实现原子性

1.操作系统如何实现原子性

在单处理器,单核,运行多线程的情况下,我们不使用线程同步工具,

我们会出现,线程之间会互相抢夺,临界区的资源,造成数据不符合我们预期的结果,

后面再说解决办法,那么我们怎么帮助实现原子性

1 屏蔽中断,不让线程之间切换,让它完成再切换

2 底层硬件自旋锁,也是不让切换的思路

在多处理器,多核,运行多线程的情况下,除了上面两点,我们该如何保持原子性呢?

让我们先了解一下 多核 cpu 之间是如何通信的:

如图,通过CPU总线 进行数据交换

问题 1

因为我们每个核心都在执行任务,当我们执行任务时,都在对同一个数据进行修改

那么到底用 哪一个数据呢?

问题 2

当 我们 使用缓存时,数据在缓存 与 磁盘上不一致时,CPU 该如何保持呢?

========================================================================

方法 1 缓存锁

一致性协议,就是解决这种问题,

所以 当我们 想使用 具有 原子性 的变量时,使用 修改(modified)  独占(exclusive) 的数据

方式 2 总线锁

CPU总线加锁 : 加锁后 , 总线只能一个核心修改数据

2.在c++中如何实现线程同步

1.使用 条件变量和锁

写一个例子:

三个线程轮流 输出 1 2 3 到 100

std::condition_variable cond;
std::mutex mtu;
int nums = 1;
void fun1()
{
    while (nums <= 100)
    {
        {
            std::unique_lock<std::mutex> lock(mtu);
            if (nums % 3 == 1)
            {
                cout << "nums 1:" << nums << endl;
                nums++;
                cond.notify_all();
            }
            else
            {
                cond.wait(lock);
            }
        }
    }
    cond.notify_all();
}
void fun2()
{
    while (nums <= 100)
    {
        {
            std::unique_lock<std::mutex> lock(mtu);
            if (nums % 3 == 2)
            {
                cout << "nums 2:" << nums << endl;
                nums++;
                cond.notify_all();
            }
            else
            {
                cond.wait(lock);
            }
        }
    }
    cond.notify_all();
}
void fun3()
{
    while (nums <= 100)
    {
        {
            std::unique_lock<std::mutex> lock(mtu);
            if (nums % 3 == 0)
            {
                cout << "nums 3:" << nums << endl;
                nums++;
                cond.notify_all();
            }
            else
            {
                cond.wait(lock);
            }
        }
    }
    cond.notify_all();
}
int main()
{
    std::thread t1(fun1);
    std::thread t2(fun2);
    std::thread t3(fun3);

    t1.join();
    t2.join();
    t3.join();
    cout << "打印完毕" << endl;
    return 0;
}

2.使用atomic


std::atomic<int> nums(1);

void print_nums(int remainder)
{
    while (nums <= 100)
    {
        int current_nums;
        do
        {
            current_nums = nums.load();
        } while (current_nums % 3 != remainder && !nums.compare_exchange_weak(current_nums, current_nums + 1));

        if (current_nums <= 100)
        {
            std::cout << "nums " << remainder + 1 << ":" << current_nums << std::endl;
        }
    }
}

int main()
{
    std::thread t1(print_nums, 1);
    std::thread t2(print_nums, 2);
    std::thread t3(print_nums, 0);

    t1.join();
    t2.join();
    t3.join();

    std::cout << "打印完毕" << std::endl;

    return 0;
}
  • 6
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值