[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Locks with IO and Thread

Nicolas Olivier

5/26/2005 3:57:00 PM


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



1 Answer

Guillaume Marcais

5/26/2005 4:38:00 PM

0

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.

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

Guillaume.