How do I avoid data races with Sendable?

In Swift, data races can lead to unpredictable behavior, especially when multiple threads access shared resources. The introduction of the Sendable protocol helps to ensure that data is safely passed across concurrent contexts. Below is an example of how to use Sendable to avoid data races in your Swift applications.

// Example of using Sendable in Swift class MyData: Sendable { var value: Int init(value: Int) { self.value = value } } let data = MyData(value: 10) // Safe use of Sendable DispatchQueue.global().async { let newValue = data.value + 1 print(newValue) }

Sendable Data Races Swift Concurrency Thread Safety Swift Programming