03
Sep
11

ruby condition variables



Added a few puts to show how condition variable works:


require 'thread'

mutex = Mutex.new
resource = ConditionVariable.new

a = Thread.new {
  mutex.synchronize {
    puts "a waiting"
    resource.wait(mutex)
    puts "a has it"
  }
}

b = Thread.new {
  mutex.synchronize {
    puts "b executing"
    resource.signal
    puts "b finished"
  }
}

[a,b].each(&:join)

The output produced is:

a waiting
b executing
b finished
a has it

Advertisements

0 Responses to “ruby condition variables”



  1. Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Blog Stats

  • 221,294 hits

%d bloggers like this: