Stderr without concurrency warnings

How can I use stderr from the Darwin module without concurrency warnings?

Here's my code in which I attempt to protect against concurrent access with a semaphore:

import class Dispatch.DispatchSemaphore
import var Darwin.stderr

let semaphore = DispatchSemaphore(value: 1) 

var stderr_safe: UnsafeMutablePointer<FILE> {
    defer { semaphore.signal() }
    semaphore.wait()
    return stderr      // warning: reference to var 'stderr'
}                      //            is not concurrency-safe

Not sure if that's all necessary, but I'm getting this warning:

reference to var 'stderr' is not concurrency-safe because it involves shared mutable state

I tried importing with the @preconcurrency attribute, but it doesn't seem to work as I still get that warning in addition to a remark:

'@preconcurrency' attribute on module 'Darwin' is unused

My goal is to report the progress of my terminal program to the user. The work is parallelized and I'm trying to use Darwin.fputs, which accepts stderr as an argument, for printing to standard error.

How can I appropriately silence this warning when compiling with -warn-concurrency?

Should be 0 (you are mimicking a mutex here).

hmm, no, 1 looks ok...

Perhaps another code (within the system itself) calls stderr unprotected as it doesn't know about your wrapper. Maybe that's that, otherwise no idea.

Would it help if you wrap stderr in a C function?

Thanks for you reply!

Good point, but I don't suppose there's anything I could do about that.

I wrapped stderr in a simple C function and it resolved all of the warnings, but I had to create a whole new target just for that. Would be nice if there were a better way.

This might be related: Concurrency warnings about system variables

1 Like