Last active
September 18, 2016 09:54
-
-
Save shotat/cc1f3fa8e1e924aa3a83cd2b9b568837 to your computer and use it in GitHub Desktop.
Lock.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Lock | |
def self.acquire | |
lock = new # リソース初期化 | |
lock.exclusive_lock! | |
if block_given? | |
yield(lock) | |
else | |
lock | |
end | |
ensure | |
if block_given? | |
lock.unlock if lock | |
end | |
end | |
end | |
Lock.acquire do |lock| | |
puts lock | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment