Pete Hodgson
Pete Hodgson

Reputation: 15845

Is there a 'standard' read/write lock implementation for ruby?

Does anyone know of an existing ruby implementation of a read/write lock - http://en.wikipedia.org/wiki/Readers-writer_lock?

Preferably this would be in a popular library or some other implementation that's been used by enough people that it's fairly bulletproof at this point.

Upvotes: 4

Views: 2138

Answers (3)

sschmeck
sschmeck

Reputation: 7685

Within concurrent-ruby gem you find Concurrent::ReadWriteLock and Concurrent::ReentrantReadWriteLock.

lock = Concurrent::ReadWriteLock.new
lock.with_read_lock  { data.retrieve }
lock.with_write_lock { data.modify! }

Upvotes: 0

Alex D
Alex D

Reputation: 30445

The link in Jonas' blog is now broken, but I have built and tested another implementation, and anybody who wants to use it is welcome to:

https://github.com/alexdowad/showcase/blob/master/ruby-threads/read_write_lock.rb

Upvotes: 1

Pesto
Pesto

Reputation: 23900

There isn't a standard one to my knowledge, but they aren't terribly hard to write. Failing that, this guy has already written one. It looks right and he's provided tests.

Upvotes: 3

Related Questions