Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>i'm pretty unclear on what is meant by this. i think the simplest implementation is simply</p> <pre><code>Hash </code></pre> <p>that is to say the built-in ruby hash <em>is</em> threadsafe if by threadsafe you mean will not blow up if > 1 threads tries to access it. this code will run safely forever</p> <pre><code>n = 4242 hash = {} loop do a = Thread.new do n.times do hash[:key] = :val end end b = Thread.new do n.times do hash.delete(:key) end end c = Thread.new do n.times do val = hash[:key] raise val.inspect unless [nil, :val].include?(val) end end a.join b.join c.join p :THREADSAFE end </code></pre> <p>i suspect by thread safe you really mean ACID - for instance a write like hash[:key]=:val followed by a read if has[:key] would return :val. but no amount of trickery with locking can provide that - the last in would always win. for example, say you have 42 thread all updating a threadsafe hash - which value should be read by the 43'rd?? surely by threasafe you don't mean some sort of total ordering on writes - therefore if 42 threads were actively writing the 'correct' value is <em>any</em> right? but ruby's built-in Hash works in just this way...</p> <p>perhaps you mean something like</p> <pre><code>hash.each do ... </code></pre> <p>in one thread and</p> <pre><code>hash.delete(key) </code></pre> <p>would not interfere with one another? i can imagine wanting that to be threadsafe, but that's not even safe in a <em>single</em> thread with the MRI ruby (obviously you cannot modify a hash while iterating over it)</p> <p>so can you be more specific about what you mean by 'threadsafe' ??</p> <p>the only way to give ACID semantics would be a gross lock (sure this could be a method that took a block - but still an external lock).</p> <p>ruby's thread scheduler isn't just going to schedule a thread smack in the middle of some arbitrary c function (like the built-in hash aref aset methods) so those are effectively threadsafe.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload