[#23231] What do you think about changing the return value of Kernel#require and Kernel#load to the source encoding of the required file? — =?ISO-8859-15?Q?Wolfgang_N=E1dasi-Donner?= <ed.odanow@...>

Dear Ruby developers and users!

8 messages 2009/04/17

[#23318] [Feature #1408] 0.1.to_r not equal to (1/10) — Heesob Park <redmine@...>

Feature #1408: 0.1.to_r not equal to (1/10)

19 messages 2009/04/26

[ruby-core:23332] [Bug #1420] synchronize does not work as expected

From: Sergio Antoy <redmine@...>
Date: 2009-04-29 21:51:37 UTC
List: ruby-core #23332
Bug #1420: synchronize does not work as expected
http://redmine.ruby-lang.org/issues/show/1420

Author: Sergio Antoy
Status: Open, Priority: Normal
ruby -v: ruby 1.8.7 (2008-08-11 patchlevel 72) [i486-linux]; Linux antoy 2.6.27-11-generic #1 SMP Wed Apr 1 20:57:48 UTC 2009 i686 GNU/Linux

=begin

There are two thread, a and b, beside the main thread.  Thread a
and b increment a global integer variable, $store, COUNT times.
Sometimes, when the program terminates, $store is not 2*COUNT.
Sometimes, it is even less than COUNT.

$store is incremented as follows: the value of $store is read into
a local variable x, a relatively long computation takes place, and
x+1 is written back into store.

Threads a and b have different priorities, thus the thread with
higher priority runs and the other suspends.  At random intervals,
the main thread swaps the priorities of the threads, thus one
thread suspends and the other resumes.  If the swap happens
between reading and writing $store, every increment of the running
thread is lost when the suspended thread resumes.

The race condition between threads a and b should be eliminated by
synchronizing the region of code that reads and writes the $store.
This is accomplished by the lines prefixed by '###'.  However, the
program does not execute as expected.

Uncomment the lines prefixed by "#t# for tracing.

=end

require 'thread'
mutex = Mutex.new

COUNT  = 20
$store = 0
DELAY  = 100000   # tune up for your hardware, about 0.2 seconds

def incr name
  (1..COUNT).each do |c|
    # comment the synchronization to expose the race condition
    # uncomment it to eliminate the race condition
    ###mutex.synchronize do
      x = $store
      #t printf("Thread %s, read  # %2d, store is %2d\n", name, c, $store)
      DELAY.times { next } 
      $store = x + 1
      #t printf("Thread %s, write # %2d, store is %2d\n", name, c, $store)
    ###end
  end
end

a = Thread.new { Thread.current.priority -= 2; incr :a }
b = Thread.new { Thread.current.priority -= 1; incr :b }
while a.alive? && b.alive? do
  a.priority, b.priority = b.priority, a.priority
  #t printf("Main thread sleeping\n")
  sleep(rand())
end

printf("%d", $store)
printf(": it should have been %d", 2*COUNT) if $store != 2*COUNT
printf("\n")


----------------------------------------
http://redmine.ruby-lang.org

In This Thread

Prev Next