When os_unfair_lock_lock() is called with C++ interop enabled, it crashes with "os_unfair_lock is corrupt". When C++ interop is disabled, it works fine. I realized OSAllocatedUnfairLock exists, but this requires macOS 13. I suspect something is happening with the os_unfair_lock() call. Here's the code, based on various online examples:
There was a bug in C++ interop until recently which prevented calling a default Swift initializer on a C struct like os_unfair_lock_s() from zero-initializing the struct, causing it to create an uninitialized junk value instead. This has been fixed recently in the main branch, but in currently released compilers you may need to zero out the memory with memset instead to work around the bug.