threads & mutex question (Rubyist book example)

S

Suraj Kurapati

Hi,

I am concerned about the lack of mutual exlusion in code listing
14.2 "Chat server using TCPServer and threads" on page 430 in "The
Well-Grounded Rubyist" book. That code goes something like this:

chatters = []

while ...
Thread.new(...) do |c|
chatters.each { ... }
chatters.push c
chatters.each { ... }
chatters.delete c
chatters.each { ... }
end
end

Multiple threads are spawned and each of them accesses the shared
resource (the chatters array) directly without mutual exclusion.

What happens when a thread inside chatters.each() gets descheduled
and another thread proceeds to add or delete an object to chatters?

Shouldn't there be a Mutex#synchronize call wrapping all access to
the chatters array?

Thanks for your consideration.
 
R

Robert Klemme

Hi,

I am concerned about the lack of mutual exlusion in code listing
14.2 "Chat server using TCPServer and threads" on page 430 in "The
Well-Grounded Rubyist" book. That code goes something like this:

chatters = []

while ...
Thread.new(...) do |c|
chatters.each { ... }
chatters.push c
chatters.each { ... }
chatters.delete c
chatters.each { ... }
end
end

Multiple threads are spawned and each of them accesses the shared
resource (the chatters array) directly without mutual exclusion.

What happens when a thread inside chatters.each() gets descheduled
and another thread proceeds to add or delete an object to chatters?

Shouldn't there be a Mutex#synchronize call wrapping all access to
the chatters array?

From what I see in your posting: yes.

Kind regards

robert
 
D

David A. Black

Hi --

Hi,

I am concerned about the lack of mutual exlusion in code listing
14.2 "Chat server using TCPServer and threads" on page 430 in "The
Well-Grounded Rubyist" book. That code goes something like this:

chatters = []

while ...
Thread.new(...) do |c|
chatters.each { ... }
chatters.push c
chatters.each { ... }
chatters.delete c
chatters.each { ... }
end
end

Multiple threads are spawned and each of them accesses the shared
resource (the chatters array) directly without mutual exclusion.

What happens when a thread inside chatters.each() gets descheduled
and another thread proceeds to add or delete an object to chatters?

Shouldn't there be a Mutex#synchronize call wrapping all access to
the chatters array?

Probably. You're right that the code as it stands runs the risk of
modifying an array while iterating over it -- which, though it
probably wouldn't do any serious damage in this case, is (I think)
undefined as to its results in Ruby, and in any case is something I
don't (consciously :) advocate doing.


David

--
David A. Black, Senior Developer, Cyrus Innovation Inc.

THE Ruby training with Black/Brown/McAnally
COMPLEAT Coming to Chicago area, June 18-19, 2010!
RUBYIST http://www.compleatrubyist.com
 
K

Keith Johnston

"Probably"? If by "probably" you mean "yes".

Won't do any "serious damage"? Since when is producing the wrong
answer not "serious" in programming?

You would be much better off making a frank admission the mistake. We
all know cognitive dissonance can be disorienting, but that's no
excuse to indulge it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top