AtomicBoolean: add compare_and_set API

未关闭
#540 7 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
3/5
预计耗时
1-2 天
新手友好度
35/100
Issue 类型
功能
描述清晰度
基本清楚
活跃度
停滞
技术栈
ruby
领域
backend

调研方向

Start by locating the AtomicBoolean implementation and its current value API. Review how the existing atomic value behavior is tested, then determine where the compare_and_set entry point belongs. Done means the proposed API supports the false-to-true use case described in the issue and its behavior is covered by the project’s tests.

由索引模型根据 Issue 内容生成。

描述

enhancement looking-for-contributor medium-priority

Compare-and-set is a useful functionality (that's why it exists in the Java API). It can save one from race conditions, as explained below.

Use case
  • Assume an atomicboolean called LOCKED, set to false.
  • Multiple concurrent threads run the following code:
if !LOCKED.value
  LOCKED.value = true
  # do something ....
  LOCKED.value = false
end

In this scenario, concurrent threads can read a false in if !LOCKED.value, and enter the if block. That defeats the purpose of an atomicboolean-backed lock.

If there was a CAS API, the fixed code would look like this:

if LOCKED.compare_and_set(false, true)

Furthermore

Ruby has no "tryLock" either. So it's not immediately obvious how to create non-blocking synchronized code. Boolean CAS would offer a nice alternative which Java programmers (and others) already are familiar with.

What do you think?

Cheers - Victor

主要语言
Ruby
星标
5.8k
派生
420
平均合并
20 小时 45 分钟
30 天内合并 PR
4

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

ruby-concurrency/concurrent-ruby 的其他 Issue

查看 ruby-concurrency/concurrent-ruby 的全部 Issue

相似的 Issue

更多 Ruby Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。