Locks with IO and Thread

Hello,

I consider the following piece of code, supposed to run under a GNU/Linux system.
The /proc/loadavg is a single line file.

#!/usr/bin/ruby

require 'thread'

file_load = File::open("/proc/loadavg", "r")
timer_mutex = Mutex::new()
timer_cond = ConditionVariable::new()

while true do
        timer_thread = Thread::new do
                sleep(1)
                timer_mutex.synchronize do
                        timer_cond.signal
                end
        end
        file_load.seek(0)
        p file_load.readlines
        timer_mutex.synchronize do
                timer_cond.wait(timer_mutex)
        end
end

The main thread is locked on "file_load.readlines".

When I switch the file to /proc/stat which is a multiple lines file, it works as expected. Each second I've got the dump.
In order to get the code work with the initial file, I've to put the main thread in critical state during the "file_load.readlines" section, which is
not really logical for me.

So my questions are:
- why everything work as expected with a multiple lines file
- why isolating the "file_load.readlines" by setting the main thread in critical state "unlock" the situation

Thanks in advance for any help,
Nicolas Olivier

That's probably your issue:
http://www.ruby-talk.org/cgi-bin/scat.rb/ruby/ruby-core/3803

Guillaume.

···

On Fri, 2005-05-27 at 00:56 +0900, Nicolas Olivier wrote:

Hello,

I consider the following piece of code, supposed to run under a GNU/Linux system.
The /proc/loadavg is a single line file.