# Threads & mutex question (Rubyist book example)

**URL:** <https://rubytalk.org/t/threads-mutex-question-rubyist-book-example/58786>\
**Category:** ruby-talk\
**Created:** [6 June 2010 05:29 UTC](https://rubytalk.org/t/threads-mutex-question-rubyist-book-example/58786 "2010-06-06T05:29:15Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Suraj\_Kurapati2](https://avatars.discourse-cdn.com/v4/letter/s/a5b964/32.png) [@Suraj\_Kurapati2](https://rubytalk.org/u/Suraj_Kurapati2)\
**Post date:** [6 June 2010 05:29 UTC](https://rubytalk.org/t/threads-mutex-question-rubyist-book-example/58786/1 "2010-06-06T05:29:15Z")

</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:

&nbsp;&nbsp;chatters = []

&nbsp;&nbsp;while ...  
&nbsp;&nbsp;&nbsp;&nbsp;Thread.new(...) do |c|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;chatters.each { ... }  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;chatters.push c  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;chatters.each { ... }  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;chatters.delete c  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;chatters.each { ... }  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;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.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [6 June 2010 11:25 UTC](https://rubytalk.org/t/threads-mutex-question-rubyist-book-example/58786/2 "2010-06-06T11:25:10Z")

</div>

From what I see in your posting: yes.

Kind regards

&nbsp;&nbsp;robert

> **···**
>
> On 06.06.2010 07:29, Suraj Kurapati wrote:
> 
> > 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:
> > 
> > &nbsp;&nbsp;chatters =
> > 
> > &nbsp;&nbsp;while ...  
> > &nbsp;&nbsp;&nbsp;&nbsp;Thread.new(...) do |c|  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;chatters.each { ... }  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;chatters.push c  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;chatters.each { ... }  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;chatters.delete c  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;chatters.each { ... }  
> > &nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;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?
> 
> --  
> remember.guy do |as, often| as.you\_can - without end  
> [http://blog.rubybestpractices.com/](http://blog.rubybestpractices.com/)

---

<div class="post-metadata">

**Author:** ![David\_A\_Black1](https://avatars.discourse-cdn.com/v4/letter/d/bcef8e/32.png) [@David\_A\_Black1](https://rubytalk.org/u/David_A_Black1)\
**Post date:** [16 June 2010 12:20 UTC](https://rubytalk.org/t/threads-mutex-question-rubyist-book-example/58786/3 "2010-06-16T12:20:19Z")

</div>

Hi --

> **···**
>
> On Sun, 6 Jun 2010, Suraj Kurapati wrote:
> 
> > 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 ...  
> > &nbsp;&nbsp;&nbsp;Thread.new(...) do |c|  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;chatters.each { ... }  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;chatters.push c  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;chatters.each { ... }  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;chatters.delete c  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;chatters.each { ... }  
> > &nbsp;&nbsp;&nbsp;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  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;COMPLEAT Coming to Chicago area, June 18-19, 2010!  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;RUBYIST [http://www.compleatrubyist.com](http://www.compleatrubyist.com)

---

<div class="post-metadata">

**Author:** ![Keith\_Johnston](https://avatars.discourse-cdn.com/v4/letter/k/7c8e57/32.png) [@Keith\_Johnston](https://rubytalk.org/u/Keith_Johnston)\
**Post date:** [16 June 2010 14:25 UTC](https://rubytalk.org/t/threads-mutex-question-rubyist-book-example/58786/4 "2010-06-16T14:25:09Z")

</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:** ![Asif\_Iqbal](https://avatars.discourse-cdn.com/v4/letter/a/c0e974/32.png) [@Asif\_Iqbal](https://rubytalk.org/u/Asif_Iqbal)\
**Post date:** [16 June 2010 14:40 UTC](https://rubytalk.org/t/threads-mutex-question-rubyist-book-example/58786/5 "2010-06-16T14:40:36Z")

</div>

mailto:ruby-talk-ctl@ruby-lang.org?body=unsubscribe

> **···**
>
> On Wed, Jun 16, 2010 at 10:28 AM, Rich McGrath Bitmob \<rich.mcgrath@bitmob.com\> wrote:
> 
> > Unsubscribe please work this Time.
> 
> --  
> Asif Iqbal  
> PGP Key: 0xE62693C5 KeyServer: pgp.mit.edu  
> A: Because it messes up the order in which people normally read text.  
> Q: Why is top-posting such a bad thing?
