• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Swift and Threads
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Swift and Threads


  • Subject: Re: Swift and Threads
  • From: "Gerriet M. Denkmann" <email@hidden>
  • Date: Tue, 13 Sep 2016 14:50:29 +0700

> On 13 Sep 2016, at 14:14, Stephen J. Butler <email@hidden> wrote:
>
> 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
>         }
>     }
> }

This is excellent: slightly faster than my original code, uses no additional memory and does not crash.
Exactly what I was looking for.

Thank your very, very much indeed!

Kind regards,

Gerriet.


_______________________________________________

Cocoa-dev mailing list (email@hidden)

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:

This email sent to email@hidden


References: 
 >Swift and Threads (From: "Gerriet M. Denkmann" <email@hidden>)
 >Re: Swift and Threads (From: Jens Alfke <email@hidden>)
 >Re: Swift and Threads (From: "Gerriet M. Denkmann" <email@hidden>)
 >Re: Swift and Threads (From: "Stephen J. Butler" <email@hidden>)

  • Prev by Date: Re: Swift and Threads
  • Next by Date: Re: Swift and Threads
  • Previous by thread: Re: Swift and Threads
  • Next by thread: Re: Swift and Threads
  • Index(es):
    • Date
    • Thread