From: "Eregon (Benoit Daloze)" Date: 2022-07-25T13:14:11+00:00 Subject: [ruby-core:109315] [Ruby master Feature#12607] Ruby needs an atomic integer Issue #12607 has been updated by Eregon (Benoit Daloze). It'd be good to revisit this (e.g. see https://twitter.com/_byroot/status/1550580128723476480). Ractor has little need for this. But with Thread there is a clear need for this. Also the STM work was not merged, so that's not a replacement either. I think @ko1's concern of "too hard to use" should be no blocker: Rubyists frequently use Threads, almost all Ruby webservers and Rails use threads nowadays, there is no point to deny that. And it is completely unrealistic to think Ractor will ever replace Threads, Ractor can only support a small subset of Ruby gems, and it will never be all or as powerful as threads. For instance I don't see Rails being able to use Ractor for parallel requests anytime soon. Regarding that example it's easy to fix, and such conditions are well understood by anyone knowing about compare-and-swap or this kind of concurrency tools. It can also be documented on the `update` method. ```ruby v = af.value af.update{ |v| v + 5 } ``` So, let's add AtomicInteger or AtomicReference (since it's more general) because it's convenient and needed by existing gems? Having it in concurrent-ruby works but it's obviously less practical, and less optimized than it could be. Also there is a trap when using Mutex as a compare-and-swap way with `try_lock`, because that leaves the Mutex locked and when the thread dies it unlocks all Mutex it had, which can be pretty slow (I found that out when reimplementing the timeout gem). ---------------------------------------- Feature #12607: Ruby needs an atomic integer https://bugs.ruby-lang.org/issues/12607#change-98453 * Author: shyouhei (Shyouhei Urabe) * Status: Feedback * Priority: Normal * Assignee: ko1 (Koichi Sasada) ---------------------------------------- (This one was derived from bug #12463) Although I don't think += would become atomic, at the same time I understand Rodrigo's needs of _easier_ counter variable that resists inter-thread tampering. I don't think ruby's Integer class can be used for that purpose for reasons (mainly because it is not designed with threads in mind). Rather we should introduce a integer class which is carefully designed. Why not import Concurrent::AtomicFixnum into core? -- https://bugs.ruby-lang.org/ Unsubscribe: