无锁 CAS 混淆

发布于 2024-11-07 05:06:26 字数 707 浏览 1 评论 0原文

嘿伙计们,
我正在阅读这些所谓的非阻塞技术,但我有一些疑问:

1)无锁操作是使用原子操作执行的,现在这些原子操作是什么?我的意思是在某种程度上他们也需要有锁,对吗?那么这种无锁方法是否只为我们提供更细粒度的锁定?
2)他们说非阻塞列表,现在非阻塞列表应该是什么:如果多个线程同时插入,那么只有一个线程会成功,另一个线程会做一些其他工作,对吧?,但是如果其他线程除了插入节点之外别无选择,那为什么它是非阻塞的呢?在前一个完成之前它不会被阻止吗?
3)在java中,它们如何进行原子操作?他们不做类似 synchronized boolean ..... 的事情吗? 那么既然它们正在获取锁,即同步部分,那么它是如何无锁的呢? 4)CAS通常用来实现原子操作。那么 cas 不是只允许对同一对象进行一项操作,并阻止(阻止)其他想要对同一对象进行操作的操作吗? 抱歉有这么多疑问... 请澄清...

编辑 当我有一个结构需要更新时会发生什么?硬件也支持吗?不正确,那么语言不会在某种程度上获取锁以使这些结构操作原子化吗?
关于JAVA:有 AtomicReference 和 AtomicReferenceFieldUpdater 类,它们提供对对象(结构或任何类型的对象)的更新,那么我们可以在性能(即速度)方面比较它们吗?两者都使用 Unsafe 类,而 Unsafe 类则使用 Native 类。

Hey guys,
I am reading these so called non-blocking techniques, but i have few doubts :

1) lock-free operation are performed using atomic operation, now what are these atomic operation ? i mean at certain level they also need to have a lock right ? so Does this lock-free approach provides us locking at finer granularity only ?

2) they say non-blocking list, Now what a non-blocking list should be : if more than one threads comes at the same insertion, only one is going to get succeed, another one will do some other work right ?, but if other thread has no choice except inserting a node then how come it is non-blocking ? won't it will get blocked until previous one is done ??

3) In java, how do they atomic operation ? doesn't they do something like synchronized boolean .....
so how it is lock-free since they are acquiring lock i.e. synchronized section ?
4) CAS is usually used to implement atomic operation. So doesn't cas allow only one operation to happen on the same object , and stops ( block ) others those want to operate on the same object ?
Sorry for so many doubts...
please clarify...

EDIT
what happens when i have a structure to update ? does it also supported by hardware ? No right, So doesn't language would be acquiring locks at some level to make these structure operation atomic ?
About JAVA : there are AtomicReference and AtomicReferenceFieldUpdater class which provides updation to a object ( structure or any kind of object) , so can we compare them in terms of performance i mean speed ? Both in tern uses Unsafe class which uses Native class .

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

记忆之渊 2024-11-14 05:06:26

这是 AtomicInteger 中的一个简单的无锁方法,

public final int getAndIncrement() {
    for (;;) {
        int current = get();
        int next = current + 1;
        if (compareAndSet(current, next))
            return current;
    }
}

您可以看到它获取值、递增它并执行比较和交换。如果compareAndSwap没有找到期望的值,则意味着另一个线程已经更改了该值。因此它会一次又一次地尝试,直到所有其他尝试更改该值的线程都已完成此操作。这是无锁的,因为没有使用锁,但不是无阻塞的,因为它可能必须多次重试(这种情况很少见)(非常罕见)


1)无锁操作是使用原子操作来执行的,那么这些原子操作是什么?我的意思是在某种程度上他们也需要有锁,对吗?那么这种无锁方法是否只能为我们提供更细粒度的锁定?

然而,锁是使用更原始的操作来实现的。否则你将需要一个锁来实现一个令人厌恶的锁。无锁方法使用原子操作来避免完全锁定。

2)他们说非阻塞列表,现在非阻塞列表应该是什么:如果多个线程同时插入,只有一个线程会成功,另一个线程会做一些其他工作,对吗? ,

如果它线程安全,他们都应该成功,一次一个。

但是如果其他线程除了插入节点之外别无选择,那么它为什么是非阻塞的呢?

术语是“并发”。它仍然必须等待另一个线程完成,它使用无锁方法来执行此操作。

在前一个完成之前它不会被阻止吗?

是的。

3)在java中,它们如何进行原子操作?

有一个调用本机方法来执行原子操作。通过阅读代码你可以看到这一点。 ;) 从生成的本机代码来看,这些本机方法被转换为机器代码指令以提高性能(而不是真正的方法调用)

他们不做类似同步布尔值的事情吗......那么既然他们正在获取锁,即同步部分,那么它是如何无锁的?

不,如果您阅读代码,您会发现事实并非如此。

4) CAS通常用来实现原子操作。所以cas不允许同一个对象上只发生一个操作,

不可以。

并阻止(阻止)其他想要对同一对象进行操作的人?

不。

再说一次,如果你看看它是如何使用的,它可能会更有意义。

Here is a simple lock free method in AtomicInteger

public final int getAndIncrement() {
    for (;;) {
        int current = get();
        int next = current + 1;
        if (compareAndSet(current, next))
            return current;
    }
}

You can see that it gets the value, increments it and does a compareAndSwap. If the compareAndSwap doesn't find the expect value, it means another thread has changed the value. So it tries again, and again, until all other threads trying to change the value have done so. This is lock free as no lock is used, but not blocking free as it may have to try again (which is rare) more than once (very rare)


1) lock-free operation are performed using atomic operation, now what are these atomic operation ? i mean at certain level they also need to have a lock right ? so Does this lock-free approach provides us locking at finer granularity only ?

However locks are implemented using more primitive operations. Otherwise you would need a lock to implement a lock adnauseum. The lock free approach uses atomic operations which avoid a full blown lock.

2) they say non-blocking list, Now what a non-blocking list should be : if more than one threads comes at the same insertion, only one is going to get succeed, another one will do some other work right ?,

If its thread safe they should both succeed, one at a time.

but if other thread has no choice except inserting a node then how come it is non-blocking ?

The term is "concurrent". It still has to wait for the other thread to finish, it uses a lock-free approach to do this.

won't it will get blocked until previous one is done ??

yes.

3) In java, how do they atomic operation ?

There is a call native method which performs the atomic operations. You can see this by reading the code. ;) From look at the native code generated, these native method are turned into machine code instructions for performance (rather than being a true method call)

doesn't they do something like synchronized boolean ..... so how it is lock-free since they are acquiring lock i.e. synchronized section ?

No, if you read the code, you would see that it doesn't.

4) CAS is usually used to implement atomic operation. So doesn't cas allow only one operation to happen on the same object ,

No.

and stops ( block ) others those want to operate on the same object ?

No.

Again, if you look at how it is used it may make more sense.

我偏爱纯白色 2024-11-14 05:06:26

1)无锁操作是使用原子操作来执行的,那么这些原子操作是什么?

例如,递增计数器包括

  1. 读取当前值、
  2. 递增存储器中的值、
  3. 写回更新的值。

原子性意味着这些都作为一个单一的、不可中断的变化发生。

我的意思是在某种程度上他们也需要有锁,对吗?

错误的。 CAS 背后的基本思想是执行上面的前两个步骤,然后在第三步之前,检查值是否在中间发生更改,如果更改则失败。然后可以稍后使用新值重试更改。

不涉及经典锁定,因为这 3 个步骤本身都是原子的。现代处理器支持第三个(比较和交换)操作,因此您可能会说它涉及寄存器级别的某种锁定(坦率地说,我不知道它到底是如何实现的) ),但无论如何,这与 Java 中锁定的一般含义不同。

CAS 的好处是提高了性能,因为即使当前 JVM 中的锁定性能有所提高,CAS 仍然更便宜,特别是在发生争用的情况下(即,当多个线程在操作上发生冲突时)。在这种情况下,使用锁,一个或多个线程被挂起,并将一个新线程带入上下文,即使不涉及交换内存,这也是一项非常昂贵的操作。

2)他们说非阻塞列表,现在非阻塞列表应该是什么

在这里您可能会混淆两个不同的术语。非阻塞列表是在插入/删除时不阻塞的列表,这通常意味着其大小不受限制(例如 CopyOnWriteArrayList)。将此与例如阻塞队列(例如ArrayBlockingQueue),它具有固定的最大大小,达到其大小限制后,其他插入调用将被阻止,直到有更多空间可用(在其他线程从队列中删除元素之后)。

使用无锁算法实现线程安全的集合(例如 ConcurrentHashMap) 与非阻塞集合不同。

1) lock-free operation are performed using atomic operation, now what are these atomic operation ?

E.g. incrementing a counter includes

  1. reading the current value,
  2. incrementing the value in memory,
  3. writing back the updated value.

Atomicity means that these all happen as one single, uniterruptible change.

i mean at certain level they also need to have a lock right ?

Wrong. The basic idea behind CAS is to do the first two steps above, then before the third, they check whether the value was changed in between, and fail if so. Then the change may be retried with the new value later.

There is no classical locking involved, as each of the 3 steps in itself is atomic. The 3rd (Compare And Swap) operation is supported by modern processors, so you may say it involves some sort of locking at the register level (to be frank, I don't know how exactly it is implemented), but at any rate, that is not the same as what is generally meant by locking in Java.

The benefit of CAS is improved performance due to the fact that even with the improved locking performance in current JVMs, CAS is still cheaper, especially in case of contention (i.e. when multiple threads do collide upon an operation). In this case, using locks, one or more of the threads are suspended, and a new thread is brought into context instead, which is a very costly operation even when it doesn't involve swapping memory.

2) they say non-blocking list, Now what a non-blocking list should be

Here you may be confusing two different terms. A non-blocking list is one which does not block on insertions/removals, which usually means its size is not bounded (e.g. CopyOnWriteArrayList). Contrast this with e.g. a blocking queue (e.g. ArrayBlockingQueue), which has a fixed maximum size, and upon reaching its size limit, additional insert calls are blocked until more space is available (after some other thread removes element(s) from the queue).

A collection which achieves thread safety using a lock-free algorithm (such as ConcurrentHashMap) is not the same as a non-blocking collection.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文