From: jonathan@... Date: 2016-11-22T18:48:04+00:00 Subject: [ruby-core:78261] [Ruby trunk Misc#11904] Why was Thread.exclusive deprecated? Issue #11904 has been updated by jonathan rochkind. I came here cause I was curious why Thread.exclusive was deprecated, and didn't find an answer! But for anyone else curious about the answer to Tony's original question (Tony has probably long since figured it out), I think you can _create_ the Mutex at 'load time', to make sure you only have one? I _think_ you can safely replace all uses of Thread.exclusive with something like this: ~~~ class RbNaCl::NaCl @init_mutex = Mutex.new class << self def init_mutex @init_mutex end end # all the rest of the definition.... end # no more: Thread.exclusive { RbNaCl::NaCl.sodium_init } RbNaCl::NaCL.init_mutex.synchronize { RbNaCl::NaCl.sodium_init } # Or of course you could include the mutex.synchronize inside the `sodium_init` implementation, # which would probably make more sense. ~~~ ---------------------------------------- Misc #11904: Why was Thread.exclusive deprecated? https://bugs.ruby-lang.org/issues/11904#change-61625 * Author: Tony Arcieri * Status: Rejected * Priority: Normal * Assignee: ---------------------------------------- See: https://bugs.ruby-lang.org/projects/ruby-trunk/repository/revisions/52554 Why was Thread.exclusive deprecated? It is useful for when you're uncertain about whether the caller is multithreaded or not, and therefore cannot initialize a mutex because the mutex must be initialized in a thread-safe context where it's not possible for multiple caller threads to initialize the mutex concurrently. One use case is here: this is an idempotent native function invoked via FFI. The contract is that it can be called repeatedly, but only by one thread at a time (concurrent calls from multiple threads can potentially corrupt its internal state): https://github.com/cryptosphere/rbnacl/blob/master/lib/rbnacl.rb#L88 Thread.exclusive is useful because it provides an implicit mutex you can ensure is initialized correctly before any other threads start. -- https://bugs.ruby-lang.org/ Unsubscribe: