Robert Ash ---------- On 24 Jan 2011, at 16:25, macruby-devel-request@lists.macosforge.org wrote:
Send MacRuby-devel mailing list submissions to macruby-devel@lists.macosforge.org
To subscribe or unsubscribe via the World Wide Web, visit http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel or, via email, send a message with subject or body 'help' to macruby-devel-request@lists.macosforge.org
You can reach the person managing the list at macruby-devel-owner@lists.macosforge.org
When replying, please edit your Subject line so it is more specific than "Re: Contents of MacRuby-devel digest..."
Today's Topics:
1. Re: Thread safety in apply example? (Matt Massicotte) 2. Re: Thread safety in apply example? (Alan Skipp) 3. Re: Thread safety in apply example? (Matt Massicotte)
----------------------------------------------------------------------
Message: 1 Date: Mon, 24 Jan 2011 07:22:44 -0800 From: Matt Massicotte <massicotte@apple.com> To: "MacRuby development discussions." <macruby-devel@lists.macosforge.org> Subject: Re: [MacRuby-devel] Thread safety in apply example? Message-ID: <48DBC4A8-59D1-42F0-8DD3-2291668D64D2@apple.com> Content-Type: text/plain; charset="us-ascii"
What's the advantage of this over just iterating over the array serially using each?
Matt
On Jan 24, 2011, at 3:40 AM, Alan Skipp wrote:
Warning, horrible hack alert! I'm sure there must be a far better implementation, but the idea is to have an array subclass which diverts all method calls through a serial dispatch queue, which should ensure thread safe access.
Anyway, here's the horrible hack. By the way, it manages to break Enumerable, other than that it's perfect!
class ThreadSafeArray < Array instance_methods.each do |meth| eval " def #{meth}(*args, &block) val = [] @serial_queue.async(@group) do val[0] = super end @group.wait val[0] end" end
def initialize(size=0, obj=nil) @serial_queue = Dispatch::Queue.new("array.serial_queue") @group = Dispatch::Group.new super end end
gcdq = Dispatch::Queue.new('doc') @result = ThreadSafeArray.new gcdq.apply(5) {|i| @result[i] = i*i } p @result #=> [0, 1, 4, 9, 16, 25]
Having to create an array for every method call to copy the return value into is terrible, is there a better way of getting a reference to the return value from a dispatch queue?
On 22 Jan 2011, at 14:52, Ernest N. Prabhakar, Ph.D. wrote:
Hi Charles,
On Jan 21, 2011, at 9:57 PM, Charles Oliver Nutter wrote:
I'm curious about this example in Queue#apply's rdoc:
* gcdq = Dispatch::Queue.new('doc') * @result = [] * gcdq.apply(5) {|i| @result[i] = i*i } * p @result #=> [0, 1, 4, 9, 16, 25]
apply is said to issue the jobs in parallel, so this would be making concurrent updates to the @result array. Are simple arrays in MacRuby thread-safe?
My assumption was that doing a parallel assign:
result[i] = i*i
would be safe, since it always accessed a unique portion of memory, but doing a serial append:
result << i*i
would not. But that may have been a mistake on my part, since the size (at least) needs to be updated. Anyone know better?
-- Ernie P.
- Charlie _______________________________________________ MacRuby-devel mailing list MacRuby-devel@lists.macosforge.org http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel
_______________________________________________ MacRuby-devel mailing list MacRuby-devel@lists.macosforge.org http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel
_______________________________________________ MacRuby-devel mailing list MacRuby-devel@lists.macosforge.org http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel
Don't be hasty. It was a horrible hack. I repent. On 24 Jan 2011, at 16:34, Robert Ash wrote:
Robert Ash ----------
On 24 Jan 2011, at 16:25, macruby-devel-request@lists.macosforge.org wrote:
Send MacRuby-devel mailing list submissions to macruby-devel@lists.macosforge.org
To subscribe or unsubscribe via the World Wide Web, visit http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel or, via email, send a message with subject or body 'help' to macruby-devel-request@lists.macosforge.org
You can reach the person managing the list at macruby-devel-owner@lists.macosforge.org
When replying, please edit your Subject line so it is more specific than "Re: Contents of MacRuby-devel digest..."
Today's Topics:
1. Re: Thread safety in apply example? (Matt Massicotte) 2. Re: Thread safety in apply example? (Alan Skipp) 3. Re: Thread safety in apply example? (Matt Massicotte)
----------------------------------------------------------------------
Message: 1 Date: Mon, 24 Jan 2011 07:22:44 -0800 From: Matt Massicotte <massicotte@apple.com> To: "MacRuby development discussions." <macruby-devel@lists.macosforge.org> Subject: Re: [MacRuby-devel] Thread safety in apply example? Message-ID: <48DBC4A8-59D1-42F0-8DD3-2291668D64D2@apple.com> Content-Type: text/plain; charset="us-ascii"
What's the advantage of this over just iterating over the array serially using each?
Matt
On Jan 24, 2011, at 3:40 AM, Alan Skipp wrote:
Warning, horrible hack alert! I'm sure there must be a far better implementation, but the idea is to have an array subclass which diverts all method calls through a serial dispatch queue, which should ensure thread safe access.
Anyway, here's the horrible hack. By the way, it manages to break Enumerable, other than that it's perfect!
class ThreadSafeArray < Array instance_methods.each do |meth| eval " def #{meth}(*args, &block) val = [] @serial_queue.async(@group) do val[0] = super end @group.wait val[0] end" end
def initialize(size=0, obj=nil) @serial_queue = Dispatch::Queue.new("array.serial_queue") @group = Dispatch::Group.new super end end
gcdq = Dispatch::Queue.new('doc') @result = ThreadSafeArray.new gcdq.apply(5) {|i| @result[i] = i*i } p @result #=> [0, 1, 4, 9, 16, 25]
Having to create an array for every method call to copy the return value into is terrible, is there a better way of getting a reference to the return value from a dispatch queue?
On 22 Jan 2011, at 14:52, Ernest N. Prabhakar, Ph.D. wrote:
Hi Charles,
On Jan 21, 2011, at 9:57 PM, Charles Oliver Nutter wrote:
I'm curious about this example in Queue#apply's rdoc:
* gcdq = Dispatch::Queue.new('doc') * @result = [] * gcdq.apply(5) {|i| @result[i] = i*i } * p @result #=> [0, 1, 4, 9, 16, 25]
apply is said to issue the jobs in parallel, so this would be making concurrent updates to the @result array. Are simple arrays in MacRuby thread-safe?
My assumption was that doing a parallel assign:
result[i] = i*i
would be safe, since it always accessed a unique portion of memory, but doing a serial append:
result << i*i
would not. But that may have been a mistake on my part, since the size (at least) needs to be updated. Anyone know better?
-- Ernie P.
- Charlie _______________________________________________ MacRuby-devel mailing list MacRuby-devel@lists.macosforge.org http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel
_______________________________________________ MacRuby-devel mailing list MacRuby-devel@lists.macosforge.org http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel
_______________________________________________ MacRuby-devel mailing list MacRuby-devel@lists.macosforge.org http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel
participants (2)
-
Alan Skipp
-
Robert Ash