Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8753965
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T13:31:49+00:00 2026-06-13T13:31:49+00:00

I have the following code ( from a Ruby tutorial ): require ‘thread’ count1

  • 0

I have the following code (from a Ruby tutorial):

require 'thread'

count1 = count2 = 0
difference = 0
counter = Thread.new do
   loop do
      count1 += 1
      count2 += 1
   end
end
spy = Thread.new do
   loop do
      difference += (count1 - count2).abs
   end
end
sleep 1

puts "count1 :  #{count1}"
puts "count2 :  #{count2}"
puts "difference : #{difference}"
counter.join(2)
spy.join(2)
puts "count1 :  #{count1}"
puts "count2 :  #{count2}"
puts "difference : #{difference}"

It’s an example for using Mutex.synchronize. On my computer, the results are quite different from the tutorial. After calling join, the counts are sometimes equal:

count1 :  5321211
count2 :  6812638
difference : 0
count1 :  27307724
count2 :  27307724
difference : 0

and sometimes not:

count1 :  4456390
count2 :  5981589
difference : 0
count1 :  25887977
count2 :  28204117
difference : 0

I don’t understand how it is possible that the difference is still 0 even though the counts show very different numbers.

The add operation probably looks like this:

val = fetch_current(count1)
add 1 to val
store val back into count1

and something similar for count2. Ruby can switch execution between threads, so it might not finish writing to a variable, but when the CPU gets back to the thread, it should continue from the line where it was interrupted, right?

And there is still just one thread that is writing into the variable. How is it possible that, inside the loop do block, count2 += 1 is executed much more times?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T13:31:50+00:00Added an answer on June 13, 2026 at 1:31 pm

    Execution of

    puts "count1 :  #{count1}"
    

    takes some time (although it may be short). It is not done in an instance. Therefore, it is not mysterious that the two consecutive lines:

    puts "count1 :  #{count1}"
    puts "count2 :  #{count2}"
    

    are showing different counts. Simply, the counter thread went though some loop cycles and incremented the counts while the first puts was executed.

    Similarly, when

    difference += (count1 - count2).abs
    

    is calculated, the counts may in principle increment while count1 is referenced before count2 is referenced. But there is no command executed within that time span, and I guess that the time it takes to refer to count1 is much shorter than the time it takes for the counter thread to go through another loop. Note that the operations done in the former is a proper subset of what is done in the latter. If the difference is significant enough, which means that counter thread had not gone through a loop cycle during the argument call for the - method, then count1 and count2 will appear as the same value.

    A prediction will be that, if you put some expensive calculation after referencing count1 but before referencing count2, then difference will show up:

    difference += (count1.tap{some_expensive_calculation} - count2).abs
    # => larger `difference`
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following ruby code: class Mp def initialize Test.new.mytest Work.new.mywork ha address
I have the following ruby code, from a learning to program book. I understand
I have the following Ruby code: require 'rubygems' require 'rest_client' url = 'http://asdf.com' response
I have the following bit of ruby code which works fine require 'WIN32OLE' excel
Im working on Ruby c extension, I have following code from c program, VALUE
I have the following code: #!/usr/bin/ruby require 'socket' server = '221.186.184.68' if ( server
I have the following code: from random import randint,choice add=lambda x:lambda y:x+y sub=lambda x:lambda
I have the following code: from selenium import selenium selenium = selenium(localhost, 4444, *chrome,
I have the following code (from https://github.com/timabell/PageStructureBuilder ) and the ever knowledgeable ReSharper is
I have the following code (inherited from someone): <div class=feedback>&nbsp;</div> <form onsubmit=return false autocomplete=off>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.