如何写一个自旋锁而不使用CAS [英] How to write a spinlock without using CAS

查看:499
本文介绍了如何写一个自旋锁而不使用CAS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

接下来是在这个问题的评论中进行的讨论

如果没有CAS操作,如何编写Spinlock?

How would one go about writing a Spinlock without CAS operations?

问题状态:

内存排序模型是这样的写入将是原子的(如果两个并发线程同时写入一个内存位置,结果将是非此即彼)。

The memory ordering model is such that writes will be atomic (if two concurrent threads write a memory location at the same time, the result will be one or the other). The platform will not support atomic compare-and-set operations.

推荐答案

维基百科的文章 spinlock 说您必须使用 Peterson's algortihm ,它使用另一个标志来指示哪个进程正在进入临界区(如果需要)。

Wikipedia's article on spinlock says you'll have to use an algorithm like Peterson's algortihm, which uses another flag to indicate which process's turn it is to enter the critical section (if desired).

这篇关于如何写一个自旋锁而不使用CAS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆