Modern concurrency tools for Ruby. Inspired byErlang,Clojure,Scala,Haskell,F#,C#,Java,and classic concurrency patterns.

The design goals of this gem are:
This gem depends oncontributions and we appreciate your help. Would you like to contribute? Great! Have a look atissues withlooking-for-contributor label. And if you pick something up let us know on the issue.
You can also get started by triaging issues which may include reproducing bug reports or asking for vital information, such as version numbers or reproduction instructions. If you would like to start triaging issues, one easy way to get started is tosubscribe to concurrent-ruby on CodeTriage.
Concurrent Ruby makes one of the strongest thread safety guarantees of any Ruby concurrency library, providing consistent behavior and guarantees on all three main Ruby interpreters(MRI/CRuby, JRuby, TruffleRuby).
Every abstraction in this library is thread safe. Specific thread safety guarantees are documented with each abstraction.
It is critical to remember, however, that Ruby is a language of mutable references.Noconcurrency library for Ruby can ever prevent the user from making thread safety mistakes (such assharing a mutable object between threads and modifying it on both threads) or from creatingdeadlocks through incorrect use of locks. All the library can do is provide safe abstractions whichencourage safe practices. Concurrent Ruby provides more safe concurrency abstractions than anyother Ruby library, many of which support the mantra of"Do not communicate by sharing memory; instead, share memory by communicating".Concurrent Ruby is also the only Ruby library which provides a full suite of thread safe andimmutable variable types and data structures.
We've also initiated discussion to document thememory model of Ruby which would provide consistent behaviour and guarantees on all three main Ruby interpreters (MRI/CRuby, JRuby, TruffleRuby).
The primary site for documentation is the automatically generatedAPI documentation which is up to date with latest release. This readme matches the master so may contain new stuff not yet released.
We also have aIRC (gitter).
concurrent-ruby usesSemantic Versioningconcurrent-ruby-ext has always same version asconcurrent-rubyconcurrent-ruby-edge will always be 0.y.z therefore followingpoint 4 applies"Major version zero (0.y.z) is for initial development. Anything may change at any time. The public API should not be considered stable." However we additionally use following rules:Future,Promise,IVar,Event,dataflow,Delay, and (partially)TimerTask into a single framework. It extensively uses the new synchronization layer to make all the featuresnon-blocking andlock-free, with the exception of obviously blocking operations like#wait,#value. It also offers better performance.Collection classes that were originally part of the (deprecated)thread_safe gem:
Concurrent::Hash.Value objects inspired by other languages:
Structure classes derived from Ruby'sStruct:
Thread-safe variables:
Deprecated features are still available and bugs are being fixed, but new features will not be added.
These are available in theconcurrent-ruby-edge companion gem.
These features are under active development and may change frequently. They are expected not tokeep backward compatibility (they may also lack tests and documentation). Semantic versions willbe obeyed though. Features developed inconcurrent-ruby-edge are expected to move toconcurrent-ruby when final.
#pop_op and#push_op.The capacity of the channel can be limited to support back pressure, use capacity option in#initialize.#pop method blocks ans#pop_op returns pending future if there is no message in the channel.If the capacity is limited the#push method blocks and#push_op returns pending future.CancellationThe Cancellation abstraction provides cooperative cancellation.
The standard methodsThread#raise ofThread#kill available in Rubyare very dangerous (see linked the blog posts bellow).Therefore concurrent-ruby provides an alternative.
It provides an object which represents a task which can be executed,the task has to get the reference to the object and periodically cooperatively check that it is not cancelled.Good practices to make tasks cancellable:
ThrottleA tool managing concurrency level of tasks.
ErlangActorActor implementation which precisely matches Erlang actor behaviour. Requires at least Ruby 2.1 otherwise it's not loaded.
WrappingExecutor A delegating executor which modifies each task before the task is given to the target executor it delegates to.
Everything within this gem can be loaded simply by requiring it:
require'concurrent'You can also require a specific abstractionpart of the public documentation since concurrent-ruby 1.2.0, for example:
require'concurrent/map'require'concurrent/atomic/atomic_reference'require'concurrent/executor/fixed_thread_pool'To use the tools in the Edge gem it must be required separately:
require'concurrent-edge'If the library does not behave as expected,Concurrent.use_simple_logger(:DEBUG) couldhelp to reveal the problem.
gem install concurrent-rubyor add the following line to Gemfile:
gem'concurrent-ruby',require:'concurrent'and runbundle install from your shell.
The Edge gem must be installed separately from the core gem:
gem install concurrent-ruby-edgeor add the following line to Gemfile:
gem'concurrent-ruby-edge',require:'concurrent-edge'and runbundle install from your shell.
Potential performance improvements may be achieved under MRI by installing optional C extensions.To minimise installation errors the C extensions are available in theconcurrent-ruby-extextension gem.concurrent-ruby andconcurrent-ruby-ext are always released together with sameversion. Simply install the extension gem too:
geminstallconcurrent-ruby-extor add the following line to Gemfile:
gem'concurrent-ruby-ext'and runbundle install from your shell.
In code it is only necessary to
require'concurrent'Theconcurrent-ruby gem will automatically detect the presence of theconcurrent-ruby-ext gemand load the appropriate C extensions.
No gems should depend onconcurrent-ruby-ext. Doing so will force C extensions on your users. Thebest practice is to depend onconcurrent-ruby and let users to decide if they want C extensions.
rbenv install jruby-9.2.17.0CONCURRENT_JRUBY_HOME to point to it, e.g./usr/local/opt/rbenv/versions/jruby-9.2.17.0bundle config get path is set, usebundle config set --local path.system true otherwise thegem name, path: '.' gems won't be found (Bundler limitation).version.rbdocs-source/signpost.md. Needs to be done only if there are visible changes in the documentation.bundle exec rake release to release the gem.It consists of['release:checks', 'release:build', 'release:test', 'release:publish'] steps.It will ask at the end before publishing anything. Steps can also be executed individually.ref gematomic andthread_safe gemsthread_safe gemto the past maintainers
and toRuby Association for sponsoring a project"Enhancing Ruby’s concurrency tooling" in 2018.
Concurrent Ruby is free software released under theMIT License.
TheConcurrent Rubylogo wasdesigned byDavid Jones. It is Copyright © 2014Jerry D'Antonio. All Rights Reserved.