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