How to access variables with compiler flag `-warn-concurrency`

When flag -warn-concurrency is specified, access to global variable warns with warning: reference to var 'name' is not concurrency-safe because it involves shared mutable state.

But access to such variables is sometimes necessary: think of stdout declared as public var stdout: UnsafeMutablePointer<FILE>.

How can I access such variable – like stdout – without warning?

Secondary question: Why does access to static variable not warn? E.g. access to variable S1.var1 defined as struct S1 { static var var1: Int = 0 } doesn't warn. Isn't this the same issue as access to global variables?

I was reading the server-side concurrency adoption guidelines, and there's a section within it that talks specifically about global variables and concurrency: sendable global variables. It's worth a quick read through, but the gist is that concurrency doesn't yet handle the checking of global variables (such as stdout) at all in Swift 5.5, so anything like that will throw a warning with the -warn-concurrency flag.

1 Like

That's great guideline. Thanks for sharing!

To get rid of the annoying warning, I created simple C function which returns stdout as FILE *. (Note that UnsafeMutablePointer itself is Codable.)

Terms of Service

Privacy Policy

Cookie Policy