When multiple threads invoke checkDeviceAndCtors, both of them may read true from the shared variable Device.HasPendingGlobals, and then invoke initLibrary redundantly. Therefore only protecting the access to Device.HasPendingGlobals is not sufficient to guarantee that initLibrary is invoked just once.
To fix this race condition, we move the invocation of initLibrary into the critical section, and remove the same lock inside initLibrary,