Difei Zhao wrote:
>   Basically, I have to implement a critical section like this to avoid
> race condition:

I think you can use Mutex. First

require 'thread'
$mutex=Mutex::new

and then

$mutex.synchronize\
{
  # your critical section
}

TPR.
-- 
Posted via http://www.ruby-forum.com/.