如何理解AKKA中使用的这种CCAS锁定机制?

发布于 2024-12-03 04:34:52 字数 1391 浏览 1 评论 0原文

我刚刚在akka中发现了一段代码。

https://codereview.scala-lang.org/fisheye/browse/~raw,r=25521/scala-svn/scala/trunk/test/files/presentation/akka/src/akka/util/LockUtil。 scala

下面列出了我感兴趣的核心方法。

/**
 * A very simple lock that uses CCAS (Compare Compare-And-Swap)
 * Does not keep track of the owner and isn't Reentrant, so don't nest and try to stick to the if*-methods
 */
class SimpleLock {
  val acquired = new AtomicBoolean(false)

  def ifPossible(perform: () => Unit): Boolean = {
    if (tryLock()) {
      try {
        perform
      } finally {
        unlock()
      }
      true
    } else false
  }



  def tryLock() = {
    if (acquired.get) false
    else acquired.compareAndSet(false, true)
  }

  def tryUnlock() = {
    acquired.compareAndSet(true, false)
  }

有两个相关的子问题。

1) 这个类 SimpleLock 的目的是什么

2) 关于它如何工作的任何提示或背景知识?

我认为由于这段代码是用 JAVA 和 scala 编写的,因此它利用了 AtomicBoolean 类。 所以我也会添加 java 标签。

欢迎任何建议!不知道为什么有人投票关闭这个问题。

相关:

可以有人能将这个 C++ 代码(来自 OpenJDK6)解释成简单的英语吗?

I just came across a piece of code in akka.

https://codereview.scala-lang.org/fisheye/browse/~raw,r=25521/scala-svn/scala/trunk/test/files/presentation/akka/src/akka/util/LockUtil.scala

The core methods I am interested in is listed below.

/**
 * A very simple lock that uses CCAS (Compare Compare-And-Swap)
 * Does not keep track of the owner and isn't Reentrant, so don't nest and try to stick to the if*-methods
 */
class SimpleLock {
  val acquired = new AtomicBoolean(false)

  def ifPossible(perform: () => Unit): Boolean = {
    if (tryLock()) {
      try {
        perform
      } finally {
        unlock()
      }
      true
    } else false
  }



  def tryLock() = {
    if (acquired.get) false
    else acquired.compareAndSet(false, true)
  }

  def tryUnlock() = {
    acquired.compareAndSet(true, false)
  }

There are two related subquestions.

1) What's purpose of this class SimpleLock

2) Any hints or background knowledge about how it works?

I think since this code is written in both JAVA and scala, it leverages the AtomicBoolean class.
So I'd add java tag also.

Any advice is welcome! Not sure why someone vote this question close.

Related:

Can anyone interpret this C++ code (from OpenJDK6) into plain English?

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

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

发布评论

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

评论(1

遮了一弯 2024-12-10 04:34:52

这是我对代码的理解。它使用获取的(AtomicBoolean)作为互斥体。如果任何线程尝试获取锁,那么它将把 acquire 设置为 true 。那么任何其他线程都无法获取锁,因为它们将从获取的锁中获取 true 并返回 false,直到该线程将获取的锁设置回 false。

由于acquireed不是来自collection,所以不会有ABA问题。所以它可以工作。

如果我错了,请纠正我。

Here is my understanding of the code. It used a acquired(AtomicBoolean) as a mutex. If any thread tries to acquire the lock , then it will set acquired to be true. then any other thread cannot acquired the lock due to they will get true from acquired and returned false until this acquired is set back to false by this thread.

Since acquired is not from a collection, it will not have ABA problem. So it can work.

Please correct me if I am wrong.

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