Skip to content
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

share one instance between multiple threads? #149

Open
abdellani opened this issue Mar 12, 2024 · 0 comments
Open

share one instance between multiple threads? #149

abdellani opened this issue Mar 12, 2024 · 0 comments

Comments

@abdellani
Copy link

Hi

is it safe to share one instance between multiple threads?
Do I need to use Mutex.new in the following context?
My answer is no, but I want to double-check.

class DLM
  include Singleton

  def initialize
    @instance = Redlock::Client.new([RedisClient.new(url: ENV['REDIS_URL'])])
    @mutex = Mutex.new
  end

  def lock(key, ttl)
    with_mutex { @instance.lock(key, ttl) }
  end

  def unlock(lock)
    with_mutex { @instance.unlock(lock) }
  end

  private

  def with_mutex(&block)
    @mutex.synchronize(&block)
  end
end
@abdellani abdellani changed the title share one instance between multiple thread? share one instance between multiple threads? Mar 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant