Thursday, February 5, 2009

an example of what NOT to do from the ruby standard library

What's wrong with Ruby's implementation of conditional variables here? Hint: what's wrong has nothing to do with synchronization, concurrency, or conditional variables.

class ConditionVariable
class Timeout < Exception; end

# Create a new timer with the argument timeout, and add the
# current thread to the list of waiters. Then the thread is
# stopped. It will be resumed when a corresponding #signal
# occurs.
def wait(timeout = nil)
@monitor.instance_eval {mon_check_owner()}
timer = create_timer(timeout)

Thread.critical = true
count = @monitor.instance_eval {mon_exit_for_cond()}
@waiters.push(Thread.current)

begin
Thread.stop
return true
rescue Timeout
return false
ensure
Thread.critical = true
begin
...


--
Love,
Fritz

No comments:

Post a Comment