This site suggests a version using withUnsafeMutableBufferPointer:

http://blog.human-friendly.com/swift-arrays-are-not-threadsafe

let nbrOfThreads = 8
let step = 2
let itemsPerThread = number * step
let bitLimit = nbrOfThreads * itemsPerThread
var bitfield = [Bool](count: bitLimit, repeatedValue: false)

let queue = dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_HIGH, 0 );

bitfield.withUnsafeMutableBufferPointer { (inout bitfieldBuffer :
UnsafeMutableBufferPointer<Bool>) -> () in
    dispatch_apply( Int(nbrOfThreads), queue ) { ( idx: size_t) -> Void in
        let startIndex = itemsPerThread * Int(idx)
        let endIndex = min( startIndex + itemsPerThread, bitLimit )
        if talk { print("Thread[\(idx)] does \(startIndex) ..<
\(endIndex)") }

        var currIndex = startIndex
        while( currIndex < endIndex )
        {
            bitfieldBuffer[currIndex] = true
            currIndex += step
        }
    }
}


On Tue, Sep 13, 2016 at 1:52 AM, Gerriet M. Denkmann <gerr...@mdenkmann.de>
wrote:

>
> > On 12 Sep 2016, at 22:49, Jens Alfke <j...@mooseyard.com> wrote:
> >
> >
> >> On Sep 12, 2016, at 6:42 AM, Gerriet M. Denkmann <gerr...@mdenkmann.de>
> wrote:
> >>
> >> So: is the code ok and the compiler broken in Debug mode?
> >> Or is the code fundamentally wrong and that it works in Release is just
> a fluke?
>
> I tried a variation of my function. This does not crash (not even in Debug
> builds), but takes twice the memory and is about four times slower:
>
> func markAndTell_2( talk: Bool, number: Int) -> [Bool]
> {
>         [...]
>         let bitLimit = nbrOfThreads * itemsPerThread
>         var bitfield = [Bool](count: bitLimit, repeatedValue: false)
>
>         var outputSlice0: ArraySlice<Bool> = []
>         [...]
>         var outputSlice7: ArraySlice<Bool> = []
>
>         let queue = dispatch_get_global_queue(
> DISPATCH_QUEUE_PRIORITY_HIGH, 0 );
>         dispatch_apply( Int(nbrOfThreads), queue,
>                 { ( idx: size_t) -> Void in
>
>                         let startIndex = itemsPerThread * Int(idx)
>                         let endIndex = min( startIndex + itemsPerThread,
> bitLimit )
>
>                         var tempSlice: ArraySlice<Bool> = bitfield[
> startIndex ..< endIndex ]
>
>                         var currIndex = startIndex
>                         while( currIndex < endIndex )
>                         {
>                                 tempSlice[currIndex] = true
>                                 currIndex += step
>                         }
>
>                         switch(idx)
>                         {
>                                 case 0: outputSlice0 = tempSlice
>                                 [...]
>                                 case 7: outputSlice7 = tempSlice
>                                 default: print("Thread[\(idx)] does not
> know where to put its slice")
>                         }
>                 }
>         )
>
>         bitfield = []
>         bitfield += outputSlice0
>         [...]
>         bitfield += outputSlice7
>
>         return bitfield
> }
>
> Gerriet.
>
>
> _______________________________________________
>
> Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)
>
> Please do not post admin requests or moderator comments to the list.
> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
>
> Help/Unsubscribe/Update your Subscription:
> https://lists.apple.com/mailman/options/cocoa-dev/
> stephen.butler%40gmail.com
>
> This email sent to stephen.but...@gmail.com
>
_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to