Any equivalent to Java's AtomicInteger?


(Rick M) #1

A lot of architectures provide CPU support for atomic increment and the like. <stdatomic.h> does, too, but most of it is unavailable in Xcode 8.1.

Is there a Swift AtomicInteger? Is that worth adding to the language?

···

--
Rick Mann
rmann@latencyzero.com


(Hooman Mehr) #2

Standard library source code already includes internal atomic counters. Introducing these apparently is on the agenda but post Swift 4.0 as part of a general language level support for concurrency. For now, the preferred API for such things is GCD (Grand Central Dispatch) provided through Dispatch module on macOS and Linux.

···

On Nov 21, 2016, at 7:55 PM, Rick Mann via swift-users <swift-users@swift.org> wrote:

A lot of architectures provide CPU support for atomic increment and the like. <stdatomic.h> does, too, but most of it is unavailable in Xcode 8.1.

Is there a Swift AtomicInteger? Is that worth adding to the language?

--
Rick Mann
rmann@latencyzero.com

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


(Rick M) #3

Standard library source code already includes internal atomic counters. Introducing these apparently is on the agenda but post Swift 4.0 as part of a general language level support for concurrency. For now, the preferred API for such things is GCD (Grand Central Dispatch) provided through Dispatch module on macOS and Linux.

That's what I'm currently doing:

  https://pastebin.com/2wKNvu2K

And for my case, performance is not a concern, but this seems like it's very expensive. Perhaps the compiler magically optimizes this down to an atomic instruction, but I doubt it.

BTW, is there any easy way to see the generated assembly? Seems to be a missing feature in Xcode.

···

On Nov 22, 2016, at 10:30 , Hooman Mehr <hooman@mac.com> wrote:

On Nov 21, 2016, at 7:55 PM, Rick Mann via swift-users <swift-users@swift.org> wrote:

A lot of architectures provide CPU support for atomic increment and the like. <stdatomic.h> does, too, but most of it is unavailable in Xcode 8.1.

Is there a Swift AtomicInteger? Is that worth adding to the language?

--
Rick Mann
rmann@latencyzero.com

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

--
Rick Mann
rmann@latencyzero.com


(Hooman Mehr) #4

As you noted, this is extremely inefficient and compiler can’t do anything about it. GCD is meant to be used as a high-level concurrency design tool. If you really need atomic integer, your best bet is writing it in C or Objective-C and calling it in Swift.

···

On Nov 22, 2016, at 4:48 PM, Rick Mann <rmann@latencyzero.com> wrote:

On Nov 22, 2016, at 10:30 , Hooman Mehr <hooman@mac.com <mailto:hooman@mac.com>> wrote:

Standard library source code already includes internal atomic counters. Introducing these apparently is on the agenda but post Swift 4.0 as part of a general language level support for concurrency. For now, the preferred API for such things is GCD (Grand Central Dispatch) provided through Dispatch module on macOS and Linux.

That's what I'm currently doing:

  https://pastebin.com/2wKNvu2K

And for my case, performance is not a concern, but this seems like it's very expensive. Perhaps the compiler magically optimizes this down to an atomic instruction, but I doubt it.

BTW, is there any easy way to see the generated assembly? Seems to be a missing feature in Xcode.

On Nov 21, 2016, at 7:55 PM, Rick Mann via swift-users <swift-users@swift.org> wrote:

A lot of architectures provide CPU support for atomic increment and the like. <stdatomic.h> does, too, but most of it is unavailable in Xcode 8.1.

Is there a Swift AtomicInteger? Is that worth adding to the language?

--
Rick Mann
rmann@latencyzero.com

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

--
Rick Mann
rmann@latencyzero.com <mailto:rmann@latencyzero.com>