Method: Cinch::Bot#synchronize
- Defined in:
- lib/cinch/bot.rb
#synchronize(name) { ... }
This method returns an undefined value.
Since Cinch uses threads, all handlers can be run simultaneously, even the same handler multiple times. This also means, that your code has to be thread-safe. Most of the time, this is not a problem, but if you are accessing stored data, you will most likely have to synchronize access to it. Instead of managing all mutexes yourself, Cinch provides a synchronize method, which takes a name and block.
Synchronize blocks with the same name share the same mutex, which means that only one of them will be executed at a time.
158 159 160 161 162 163 |
# File 'lib/cinch/bot.rb', line 158 def synchronize(name, &block) # Must run the default block +/ fetch in a thread safe way in order to # ensure we always get the same mutex for a given name. semaphore = @semaphores_mutex.synchronize { @semaphores[name] } semaphore.synchronize(&block) end |