# Concurrent work on Array

**URL:** <https://forums.swift.org/t/concurrent-work-on-array/79276>\
**Category:** Using Swift\
**Created:** [April 15, 2025, 10:42am UTC](https://forums.swift.org/t/concurrent-work-on-array/79276 "2025-04-15T10:42:20Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![gerriet](https://avatars.discourse-cdn.com/v4/letter/g/dc4da7/32.png) [@gerriet](https://forums.swift.org/u/gerriet)\
**Post date:** [April 15, 2025, 10:42am UTC](https://forums.swift.org/t/concurrent-work-on-array/79276/1 "2025-04-15T10:42:20Z")

</div>

Is is possible to partition an array into non-overlapping parts in a way the compiler recognizes this fact and thus knows that there is no possibility of a data race?

```
func doTest() async
{
	let arrSize = 10_000_000_000
	var byteArray: [UInt8] = Array(repeating: 0, count: arrSize)
	let bytesPertask = 10_000_000
	let nbrTasks = arrSize / bytesPertask
	
	#if true	//	Swift 6: does not compile
	await withTaskGroup 
	{	taskGroup in	
	
		for i in 0 ..< nbrTasks	 
		{
			taskGroup.addTask()
			{
				/*	Swift 6 Compile Error: 
					Passing closure as a 'sending' parameter risks causing data races between code   
					in the current task and concurrent execution of the closure
				**/
				let startIndex = i * bytesPertask
				let endIndex = min( startIndex + bytesPertask, arrSize)
				
				for j in startIndex ..< endIndex { byteArray[j] = 1 }
			}
		}
		
		await taskGroup.waitForAll()
	}
	#else //	Swift 5 or 6: compiles with Warning 
	byteArray.withUnsafeMutableBufferPointer
	{	pointer in	
	
		guard let bytePointer = pointer.baseAddress else	{return}
		DispatchQueue.concurrentPerform( iterations: nbrTasks)
		{
			i in
			
			let startIndex = i * bytesPertask
			let endIndex = min( startIndex + bytesPertask, arrSize)
			
			/*	Compile Warning: 
				Capture of 'bytePointer' with non-sendable type 'UnsafeMutablePointer<UInt8>' in 
				a '@Sendable' closure
			**/
			for j in startIndex ..< endIndex { bytePointer[j] = 1 }
		}
	}
	#endif
}

```

How to get rid of the warning with concurrentPerform?  
How to make TaskGroup compile in Swift Language Mode 6?
