solnic / coercible

Powerful, flexible and configurable coercion library. And nothing more.
MIT License
137 stars 16 forks source link

Question: Thread safety #12

Closed reidmorrison closed 10 years ago

reidmorrison commented 10 years ago

Is coercible thread-safe at an instance level, or should we use a new coercible instance per thread?

Specifically when running JRuby which has real threads without an interpreter lock.

Using the example below:

coercer = Coercible::Coercer.new

# coerce a string to a date
coercer[String].to_date('2012/12/25')

# Fetch the string coercer once
string_coercer = coercer[String]
string_coercer.to_date('2012/12/25')

Can one store the 'coercer' instance above into a class or global variable and then use it concurrently from multiple threads?

Also, can multiple threads access the same 'string_coercer' instance across threads?

mbj commented 10 years ago

@reidmorrison From what I know: Coercible does not have mutable state at the instance level. All intermediaries (if any) are stored on the stack. So it should be thread-save / re-entrant.