Is Ruby Array#[]= threadsafe for a preallocated array? Can this be made lockless? -


I wrote some code in Ruby to process the items in a thread. In this process, I have undo a result array which is the same size as the pass-array. Within Threadpool, I'm specifying items in the already assigned array, but those items are guaranteed to be indexed unique. Keeping this in mind, do I get to Mutex # Synchronize ?

Need to surround the assignment with an example? Example:

  size = 1000000000 def collect_via_threadpool (item, pool_count = 10) processed_items = array.new (items.count, zero) index = -1 length = item.lampion mutex = mute x New Items_Mutex = Mute x. New [Pool_Cent, Length, 50] Thread.min.times.collect On startup (i = mute x.synchronize {index = index + 1}) & lt; Length is processed Iitems [i] = yield (item [i]) # ^ I need to synchronize around this? `Processed_items` interval end end end I want (and: included) processed_items end items = collect_via_threadpool (SIZE.times.to_a, 100) what do we do. Items | Item.to_s do not extend till the end of the item. Size == SIZE items are not.exe_wind_index. Items, index | Unless the item.to_i == index returns the 'success', it will take longer to run  

(this test code takes a long time, but every time 'success' is printed .)

It seems that I want to keep mute x # synchronize with array # [] = , but my question is:

Inside Ruby's specification Is this code safe?

There is nothing in it Ruby specified to thread Mutex (and thus nothing received). If you want to know that your specific code thread is safe, you will need to see how your implementation handles threads and arrays.

For MRI, Array .new (n, nil) actually allocates memory for the entire array, so if your thread is guaranteed that your code will not index , Then it will work. It is very safe because many threads work on different aspects without mute x.

However for other implementations, Array.new (n, zero) can not allocate the entire array, and later include refinance and memory copies to specify the indices Which can break frighteningly.

It is, while your code (at least in MRI) can work, do not trust it when we are on this topic, the sources of Ruby actually In parallel run are not specified because if you are trying to avoid mutes because you feel that you can promote some performance, then you should redo your view.


Comments

Popular posts from this blog

java - Can't add JTree to JPanel of a JInternalFrame -

javascript - data.match(var) not working it seems -

javascript - How can I pause a jQuery .each() loop, while waiting for user input? -