-
Notifications
You must be signed in to change notification settings - Fork 419
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
should the thread_local_var_accessor gem be included within the concurrent-ruby gem? #1056
Comments
Thanks for writing up the proposal. I took a quick look at the code in https://github.com/aks/thread_local_var_accessors. class Foo
tlv_accessor: :tvar
def initialize
end
def foo
self.tvar = true
p tvar
end
foo = Foo.new
8.times.map { Thread.new { foo.foo } } Some threads might print |
Given this seems mostly convenience on top of And the fact the |
I corrected your test example above, and ran it with thousands of threads. At no point was there ever any error. However, I'm fine keeping the library in a separate gem. Here's the updated test program and a sample output: % cat tlvtest.rb
#!/usr/bin/env ruby
require 'thread_local_var_accessors'
class Foo
include ThreadLocalVarAccessors
tlv_accessor :tvar
def initialize
end
def foo(num)
self.tvar = num
tvar
end
end
obj = Foo.new
count = ARGV.shift&.to_i || 100
puts "Count: #{count}"
data_in = count.times.to_a
data_out = data_in.map { |num| Thread.new { obj.foo(num) }.run.value }
diffs = data_in - data_out
if diffs.size.zero?
puts "no diffs"
else
puts "Diffs: #{diffs}"
end
And, here's the output: % $ time ./tlvtest.rb 100000
Count: 100000
no diffs
./tlvtest.rb 100000 2.11s user 1.47s system 113% cpu 3.145 total |
I've written a little gem that makes concurrent-ruby's thread-local variables easy to use, in the same manner of
attr_reader, attr_writer, attr_accessor
: it is called:thread_local_var_accessors
See:
Eg:
I've been using it in my company software for some time now. It is an easy way to encourage developers to improve the thread-safetyness of their code, especially code that runs in a multi-threaded environment (eg: sidekiq tasks).
I'm happy to continue maintaining it separately, but I'm also happy to contribute it to the core concurrent-ruby gem, if that's appropriate. In the latter case, since we are already using concurrent-ruby elsewhere, for other reasons, it would mean one gem less to track for us. 😄
The text was updated successfully, but these errors were encountered: