CAS操作和锁的区别

当更新的目标是一个字时,可以采用比使用锁更有效的方法来更新内存。这些替代方法是基于现代处理器中实现的原子或互锁指令。这些通常被称为CAS(Compare And Swap)操作,例如x86上的 "lock cmpxchg"。CAS操作是一种特殊的机器码指令,它允许内存中的一个字被有条件地设置为一个原子操作。对于 "增加一个计数器的实验",每个线程可以在一个循环中读取计数器,然后尝试将其原子化地设置为新的增加值。新旧值作为参数提供给这条指令。如果当操作被执行时,计数器的值与提供的预期值一致,那么计数器就会被更新为新的值。另一方面,如果该值不符合预期,CAS操作将失败。这时,试图执行改变的线程就会重试,重新读取计数器的增量,以此类推,直到改变成功。这种CAS方法比锁的效率高得多,因为它不需要向内核切换上下文来进行仲裁。然而,CAS操作并不是没有成本的。处理器必须锁定其指令流水线以确保原子性,并采用内存屏障来使其他线程看到这些变化。在Java中,通过使用java.util.concurrent.Atomic*类,可以进行CAS操作。

如果程序的关键部分比一个计数器的简单增量更复杂,可能需要一个复杂的状态机,使用多个CAS操作来协调争夺。使用锁来开发并发程序是很困难的;使用CAS操作和内存屏障来开发无锁算法要复杂很多倍,而且很难证明它们是正确的。

理想的算法是只有一个线程拥有对单一资源的所有写入,其他线程读取结果的算法。在多处理器环境中读取结果需要内存屏障,以使在其他处理器上运行的线程看到这些变化。

A more efficient alternative to the use of locks can be employed for updating memory when the target of the update is a single word. These alternatives are based upon the atomic, or interlocked, instructions implemented in modern processors. These are commonly known as CAS (Compare And Swap) operations, e.g. “lock cmpxchg” on x86. A CAS operation is a special machine-code instruction that allows a word in memory to be conditionally set as an atomic operation. For the “increment a counter experiment” each thread can spin in a loop reading the counter then try to atomically set it to its new incremented value. The old and new values are provided as parameters to this instruction. If, when the operation is executed, the value of the counter matches the supplied expected value, the counter is updated with the new value. If, on the other hand, the value is not as expected, the CAS operation will fail. It is then up to the thread attempting to perform the change to retry, re-reading the counter incrementing from that value and so on until the change succeeds. This CAS approach is significantly more efficient than locks because it does not require a context switch to the kernel for arbitration. However CAS operations are not free of cost. The processor must lock its instruction pipeline to ensure atomicity and employ a memory barrier to make the changes visible to other threads. CAS operations are available in Java by using the java.util.concurrent.Atomic* classes.

If the critical section of the program is more complex than a simple increment of a counter it may take a complex state machine using multiple CAS operations to orchestrate the contention. Developing concurrent programs using locks is difficult; developing lock-free algorithms using CAS operations and memory barriers is many times more complex and it is very difficult to prove that they are correct.

The ideal algorithm would be one with only a single thread owning all writes to a single resource with other threads reading the results. To read the results in a multi-processor environment requires memory barriers to make the changes visible to threads running on other processors.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: CAS和synchronized都是用于实现多线程同步的机制,但它们的实现方式和应用场景有所不同。 CAS(Compare And Swap)是一种乐观机制,它通过比较内存中的值和期望值是否相等来判断是否需要更新,如果相等则更新,否则不更新。CAS的优点是无,可以避免线程的阻塞和唤醒,适用于并发量较大的情况。 synchronized是一种悲观机制,它通过获取来保证同一时刻只有一个线程可以执行临界区代码,其他线程需要等待的释放才能执行。synchronized的优点是可以保证线程安全,但缺点是会造成线程的阻塞和唤醒,适用于并发量较小的情况。 因此,CAS适用于并发量较大的情况,可以提高程序的性能;而synchronized适用于并发量较小的情况,可以保证程序的正确性。 ### 回答2: CAS(Compare and Swap)和synchronized都属于并发编程中用于实现线程安全的技术,但它们有一些重要的区别。 首先,CAS是一种乐观技术,而synchronized是一种悲观技术。CAS通过对比内存中的值与期望值,根据比较结果来决定是否更新内存中的值,无期间没有线程阻塞。相比之下,synchronized通过获取对象的来保证线程的同步执行,如果无法获取,线程会进入阻塞状态,直到获取到才能继续执行。 其次,CAS是基于底层硬件支持的原子操作指令来实现的,效率更高。而synchronized是基于Java的关键字实现的,需要涉及到用户态和内核态的切换,效率相对较低。 此外,CAS操作可以针对单个变量进行,并且只有在比较结果一致的情况下才会进行更新,适合对于多个线程并发更新同一个变量的情况。而synchronized可以用于对代码块或方法进行同步,可以保护多个线程访问共享资源的一致性。 最后,CAS相对于synchronized更容易产生ABA问题。ABA问题是指在CAS操作中,如果变量的值在操作过程中被其他线程修改过多次,并且最后又变回原来的值,那么CAS操作会错误地认为没有被修改过。为了解决ABA问题,Java提供了AtomicStampedReference和AtomicMarkableReference等类。 总的来说,CAS适用于对单个变量进行高效的并发更新,而synchronized适用于保护共享资源的一致性以及对代码块或方法进行同步。根据实际需求和场景的不同,可以选择合适的技术来实现线程安全。 ### 回答3: CAS(Compare and Swap)和synchronized是两种用于实现线程安全的机制。 CAS是一种乐观机制,它使用原子操作来实现线程安全。CAS包含三个操作数:内存位置V、旧的预期值A和要更新的新值B。当执行CAS操作时,只有在内存位置的值与预期值相等时,CAS才会修改内存位置的值为新值B;否则,不做任何操作。由于CAS操作是原子的,所以能够保证只有一个线程能够成功地修改内存位置的值。但如果多个线程同时执行CAS操作,只有一个线程会成功,其他线程需要重试。 synchronized是一种悲观机制,它使用互斥来实现线程安全。在使用synchronized关键字修饰的代码块或方法中,同一时刻只有一个线程可以执行该代码块或方法。其他线程需要等待上一个线程执行完毕并释放后才能执行。synchronized能够保证代码块或方法的互斥访问,避免了并发访问的问题。但由于每次只能有一个线程执行,其他线程会进入阻塞状态,可能会造成性能问题。 总的来说,CAS是一种非阻塞的乐观机制,适用于竞争不激烈的情况,能够提高并发性能。而synchronized是一种阻塞的悲观机制,适用于竞争激烈的情况,能够保证线程安全,但可能降低并发性能。在实际开发中,我们需要根据具体的场景选择适合的机制来确保线程安全和并发性能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值