# Threads & mutex question (Rubyist book example)

**URL:** <https://www.ruby-forum.com/t/threads-mutex-question-rubyist-book-example/189722>\
**Category:** Ruby\
**Created:** [June 6, 2010, 7:29am UTC](https://www.ruby-forum.com/t/threads-mutex-question-rubyist-book-example/189722 "2010-06-06T07:29:10Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![snk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d9b06d/32.png) [@snk](https://www.ruby-forum.com/u/snk)\
**Post date:** [June 6, 2010, 7:29am UTC](https://www.ruby-forum.com/t/threads-mutex-question-rubyist-book-example/189722/1 "2010-06-06T07:29:10Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![snk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d9b06d/32.png) [@snk](https://www.ruby-forum.com/u/snk)\
**Post date:** [June 6, 2010, 1:25pm UTC](https://www.ruby-forum.com/t/threads-mutex-question-rubyist-book-example/189722/2 "2010-06-06T13:25:34Z")

</div>

On 06.06.2010 07:29, Suraj K. wrote:

> ```
> chatters.each { ... }
> 
> ```
> 
> 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

---

<div class="post-metadata">

**Author:** ![snk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d9b06d/32.png) [@snk](https://www.ruby-forum.com/u/snk)\
**Post date:** [June 17, 2010, 4:22pm UTC](https://www.ruby-forum.com/t/threads-mutex-question-rubyist-book-example/189722/3 "2010-06-17T16:22:21Z")

</div>

Hi –

On Sun, 6 Jun 2010, Suraj K. wrote:

> ```
> chatters.each { ... }
> 
> ```
> 
> 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](http://www.compleatrubyist.com)

---

<div class="post-metadata">

**Author:** ![snk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d9b06d/32.png) [@snk](https://www.ruby-forum.com/u/snk)\
**Post date:** [June 17, 2010, 4:22pm UTC](https://www.ruby-forum.com/t/threads-mutex-question-rubyist-book-example/189722/4 "2010-06-17T16:22:23Z")

</div>

“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.

---

<div class="post-metadata">

**Author:** ![snk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d9b06d/32.png) [@snk](https://www.ruby-forum.com/u/snk)\
**Post date:** [June 17, 2010, 4:22pm UTC](https://www.ruby-forum.com/t/threads-mutex-question-rubyist-book-example/189722/5 "2010-06-17T16:22:32Z")

</div>

Unsubscribe please work this Time.

---

<div class="post-metadata">

**Author:** ![snk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d9b06d/32.png) [@snk](https://www.ruby-forum.com/u/snk)\
**Post date:** [June 17, 2010, 4:26pm UTC](https://www.ruby-forum.com/t/threads-mutex-question-rubyist-book-example/189722/6 "2010-06-17T16:26:23Z")

</div>

On Wed, Jun 16, 2010 at 10:28 AM, Rich McGrath Bitmob  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Unsubscribe please work this Time.

[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)?body=unsubscribe
