Threading locks for variable access in Swift

I'm new to the threading topic with Swift and would appreciate help in deciding the best approach to locking access to a variable in a package when multiple threads may be accessing the package.

For example, assume a variable defines pi with a given accuracy. I need to lock access to this variable while it is being calculated with a certain precision. I can't guarantee the user of my package will be an actor or have appropriate locks in place. What would be the best approach to use?

I think global and static variable initializations are automatically made thread-safe by the language (calling into dispatch_once internally).

2 Likes

Didn't know that. Thanks for the heads up. In a similar application, a Java library was using locks, so nice that Swift does this automatically.