Locks with IO and Thread

N

Nicolas Olivier

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::eek:pen("/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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top