Get rid of Swift warning for concurrent array access
10:54 21 Dec 2025

I have that code, which works fine. But the Swift compiler raises a warning. I know the warning is true, and understand why the compiler warns me. But the code in itself takes care of that.

Is there any way to silence this warning (and I take responsbility for it) - or better, a Swift way to do the same thing without warning.

public func ColorFunction2DImage(color2D: ColorFunction2D,
                                 size:    CGSize) -> CGImage {
    
    let width           = Int(size.width)
    let height          = Int(size.height)
    
    //  RGBA8 buffer (4 bytes/pixel)
    var pixels = [UInt32](repeating: 0,
                          count:     width * height)
    
    let chunkHeight             = 64    //  I hesitated with some adjustable threshold but...
    let chunkCount              = (height + chunkHeight - 1) / chunkHeight

    DispatchQueue.concurrentPerform(iterations: chunkCount) { chunkIndex in
        let yStart  = chunkIndex * chunkHeight
        let yEnd    = min(yStart + chunkHeight, height)  // The last chunck might be smaller

        for y in yStart..

The warning is Mutation of captured var 'pixels' in concurrently-executing code at line

pixels[rowStartIndex + x]   = color

As you can see, I start several threads in parallel, each taking care of a slice of the array. The threads do write the same array at the same time, but never at the same index.

swift suppress-warnings