Thread synchronization

M

Marcin Tyman

Hi guys,
I've got issue with this example part of code:
stationStatsTr = Thread.new do
while(true)
puts "stationStatsTr\n"
sleep 1
end
end

rulesTr = Thread.new do
while(true)
#puts stationStatsTr.status
sleep 1
stationStatsTr.stop() if stationStatsTr.status == "run"
puts "rulesTr\n"
sleep 1
end
end



stationStatsTr.join(10)
rulesTr.join(10)

#the output from this is as follows
stationStatsTr
stationStatsTr
stationStatsTr
stationStatsTr
stationStatsTr
stationStatsTr
stationStatsTr
stationStatsTr
stationStatsTr
stationStatsTr
C:/Documents and
Settings/PROXIMETRY/Desktop/Autotests/Fake_DDL/testing.rb:12: undefined
method `stop' for #<Thread:0x2bb79dc dead> (NoMethodError)
from C:/Documents and
Settings/PROXIMETRY/Desktop/Autotests/Fake_DDL/testing.rb:22:in `join'
from C:/Documents and
Settings/PROXIMETRY/Desktop/Autotests/Fake_DDL/testing.rb:22



#####################################################################
REPLACING stationStatsTr.stop() by stationStatsTr.kill works as expected
(stationStats is killed and rulesTr is running). But stop() method
cannot stop stationStatsTr. Moreover rulesTr doesn't run (as you can see
in output) and error occured at the end of script (after set 10 secs in
join).
What is going on with this. The idea of the part of code is that only
one of the threads should run simultaneously. The rulesTr should be able
to stop and start staionStatsTr.

Any help will be helpful.
Thanks in advance,
MT
 

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

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top