#20395 (comment) reminded me once again of the awkwardness of runtime.UnlockOSThread.
As currently documented, UnlockOSThread is not safe for use in any library code: a library that calls LockOSThread has no way to know whether the caller also called LockOSThread (and hence expects the thread to remain locked). In practice, UnlockOSThread is only safe to use within the package that spawned the goroutine to be locked.
UnlockOSThread could be made safe for use in libraries with one simple change: after N calls to LockOSThread, do not actually unlock the thread until the Nth call to UnlockOSThread. This would make the API a bit easier to use correctly and eliminate a potential source of subtle concurrency bugs.
#20395 (comment) reminded me once again of the awkwardness of
runtime.UnlockOSThread.As currently documented,
UnlockOSThreadis not safe for use in any library code: a library that callsLockOSThreadhas no way to know whether the caller also calledLockOSThread(and hence expects the thread to remain locked). In practice,UnlockOSThreadis only safe to use within the package that spawned the goroutine to be locked.UnlockOSThreadcould be made safe for use in libraries with one simple change: after N calls toLockOSThread, do not actually unlock the thread until the Nth call toUnlockOSThread. This would make the API a bit easier to use correctly and eliminate a potential source of subtle concurrency bugs.