IT Community - Software Programming, Web Development and Technical Support

MultiThreading in Ruby

This is a discussion on MultiThreading in Ruby within the Ruby forums, part of the Web Development category; Can anybody tell me how to use Multithreading in Ruby?...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Web Development > Ruby

Register FAQ Members List Calendar Mark Forums Read
  #1  
Old 10-12-2007, 11:14 PM
S.Vinothkumar S.Vinothkumar is offline
D-Web Genius
 
Join Date: May 2007
Posts: 905
S.Vinothkumar is on a distinguished road
Question MultiThreading in Ruby

Can anybody tell me how to use Multithreading in Ruby?
__________________
S.VinothkumaR
Behind me is infinite power,
Before me is Endless Possibility,
Around me is Boundless Opportunity,
Why should I fear!
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2  
Old 10-17-2007, 07:53 AM
S.Vinothkumar S.Vinothkumar is offline
D-Web Genius
 
Join Date: May 2007
Posts: 905
S.Vinothkumar is on a distinguished road
Question Re: MultiThreading in Ruby

Hi,

I have 2 seperate threads (which do different things) created but how do I run them together?
__________________
S.VinothkumaR
Behind me is infinite power,
Before me is Endless Possibility,
Around me is Boundless Opportunity,
Why should I fear!
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3  
Old 10-19-2007, 01:50 AM
leoraja8 leoraja8 is offline
D-Web Sr.Programmer
 
Join Date: May 2007
Posts: 190
leoraja8 is on a distinguished road
Question Re: MultiThreading in Ruby

By "run them together" do you mean end one of the threads? Or do you mean simply to have them run concurrently?
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4  
Old 11-04-2007, 11:12 PM
S.Vinothkumar S.Vinothkumar is offline
D-Web Genius
 
Join Date: May 2007
Posts: 905
S.Vinothkumar is on a distinguished road
Default Re: MultiThreading in Ruby

I want to run both the threads concurrently. The code is supposed to filter data from a queue and delete any redundant data. I would like it to enqueue new data while filtering and dequeue any redundant data at the same time. Of course the filtering part would need to wait until there is a certain number of objects in the queue before I can start deqeueing. I have seen both those resources but I still can't figure them out cos I am new with Ruby.
__________________
S.VinothkumaR
Behind me is infinite power,
Before me is Endless Possibility,
Around me is Boundless Opportunity,
Why should I fear!
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5  
Old 11-05-2007, 04:57 AM
leoraja8 leoraja8 is offline
D-Web Sr.Programmer
 
Join Date: May 2007
Posts: 190
leoraja8 is on a distinguished road
Default Re: MultiThreading in Ruby

Oh..fine…Can u do one thing?

Can u post here what you have tried?
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6  
Old 11-05-2007, 11:16 PM
S.Vinothkumar S.Vinothkumar is offline
D-Web Genius
 
Join Date: May 2007
Posts: 905
S.Vinothkumar is on a distinguished road
Default Re: MultiThreading in Ruby

Ya..sure…

I've tried 2 things so far. First I defined each thread as function by itself.


First is...

Code:
Thread.new do
#read input file, check input and queue to a queue
end
end
Second one is...

Code:
Thread.new do
#dequeue first 10 object from queue and enqueue to a new queue
#delete redundant object from new and previous queue using a loop
end
end
I think the program couldn't run this way. I can't really recall cos I did this a while back. Is the syntax for this correct? If so how would I run the 2 threads now?
__________________
S.VinothkumaR
Behind me is infinite power,
Before me is Endless Possibility,
Around me is Boundless Opportunity,
Why should I fear!
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7  
Old 11-05-2007, 11:25 PM
S.Vinothkumar S.Vinothkumar is offline
D-Web Genius
 
Join Date: May 2007
Posts: 905
S.Vinothkumar is on a distinguished road
Cool Re: MultiThreading in Ruby

I also tried as follows,

Code:
a=Thread.new do
#same as 1st thread above
end

b=Thread.new do
#same as 2nd thread above
end
What I've tried with this code is using the Thread.pass function which passes control to the other thread once the current thread has finished one loop. (Both threads havely while loops which loop until (i) input file has finished reading (ii) There are less than 10 objects in queue to compare with) When I now execute the program it runs but are there is no output. (The program has some output when I run the 2 threads normally in series) Is there something missing code to run the 2 threads in parallel? Sorry if the answer seems obvious, as I've never actually done ruby/multithreading.
__________________
S.VinothkumaR
Behind me is infinite power,
Before me is Endless Possibility,
Around me is Boundless Opportunity,
Why should I fear!
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8  
Old 11-12-2007, 10:13 PM
leoraja8 leoraja8 is offline
D-Web Sr.Programmer
 
Join Date: May 2007
Posts: 190
leoraja8 is on a distinguished road
Cool Re: MultiThreading in Ruby

Wow..fine…

If u post the full code that will help to find the solution…can u post ur full code here?
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9  
Old 11-20-2007, 12:49 AM
S.Vinothkumar S.Vinothkumar is offline
D-Web Genius
 
Join Date: May 2007
Posts: 905
S.Vinothkumar is on a distinguished road
Smile Re: MultiThreading in Ruby

Ya..sure..

check it out the following code....

Code:
class FnC

require 'EPC'
require 'Queue'

#defining & initialising constants
BITS64LENGTH=65
BITS64HEADER=2.to_s(2)

BITS96LENGTH=97
BITS96HEADER="00"+48.to_s(2)

BITS96PARTITION0="00"+0.to_s(2)
BITS96PARTITION1="00"+1.to_s(2)
BITS96PARTITION2="0"+2.to_s(2)

a=Thread.new { 
#create a queue to store EPC for filtering purposes
$queue=Queue.new
#create a queue counter which starts at 0
$queueTotal=0

#read input file which is used as virtual reader
File.open("Input.txt") do |file|

#create an EPC object
EPCtest=EPC.new

#read input file until there is no more EPC
while $scanEPC=file.gets

#if EPC is 64 bits in length
if $scanEPC.length==BITS64LENGTH 

#set bits and header of EPC from virtual reader to the EPC object 
EPCtest.setbits(BITS64LENGTH)
EPCtest.setheader($scanEPC,0,1)

#if header bits are 10 in binary
if EPCtest.getheader()==BITS64HEADER 

#set filter value of EPC from virtual reader to the EPC object
EPCtest.setfilter($scanEPC,0,2)
#enqueue remaining bits to queue
$queue.enqueue($scanEPC)
#increase queue counter by 1
$queueTotal+=1
Thread.pass

#else the EPC is invalid
else
print "The EPC "+$scanEPC+" is not valid \n"
end

#else if EPC is 96 bits in length
elsif $scanEPC.length==BITS96LENGTH

#set bits and header of EPC from virtual reader to the EPC object
EPCtest.setbits(BITS96LENGTH)
EPCtest.setheader($scanEPC,0,7)

#if header bits are 00110000 in binary
if EPCtest.getheader()==BITS96HEADER

#set filter value and partition of EPC from virtual reader to the EPC object
EPCtest.setfilter($scanEPC,0,2)
EPCtest.setpartition($scanEPC,0,2)

case EPCtest.getpartition()

#partition value is 000
when BITS96PARTITION0
#enqueue remaining bits to queue
$queue.enqueue($scanEPC)
#increase queue counter by 1
$queueTotal+=1 
Thread.pass

#partition value is 001
when BITS96PARTITION1
#enqueue remaining bits to queue
$queue.enqueue($scanEPC)
#increase queue counter by 1
$queueTotal+=1 
Thread.pass

#partition value is 010
when BITS96PARTITION2 
#enqueue remaining bits to queue
$queue.enqueue($scanEPC)
#increase queue counter by 1
$queueTotal+=1
Thread.pass

#else the EPC is invalid
else 
puts "The EPC "+$scanEPC+" is not valid \n"
end 

#else the EPC is invalid
else
puts "The EPC "+$scanEPC+" is not valid \n" 
end

#else the EPC is invalid
else 
puts "The EPC "+$scanEPC+" is not valid \n"
end
end 
end
}

b=Thread.new {

#filter until there is less than 10 EPC in the queue
while $queueTotal>=10

#store the first 10 EPC of the queue in another comparing queue
$queueCompare=$queue.peek(10)
#create a queueCompare counter which starts at 1
$queueCompareCounter=1

#filter until we have compared with all 10 EPC's in the queue
while $queueCompareCounter<10

#if similar EPC 
if $queueCompare[0]==$queueCompare[$queueCompareCounter]
#delete the repeated EPC from the queue and the comparing queue 
$queueCompare.delete_at($queueCompareCounter)
$queue.delete_at($queueCompareCounter)
#decrease queue counter by 1
$queueTotal-=1

#else we increase the queueCompare counter by 1 to check next EPC
else 
$queueCompareCounter+=1
end
end 

#dequeue first EPC from the queue which is unique
$EPCdequeue=$queue.dequeue

#if a 64bit EPC
if EPCtest.getbits()==BITS64LENGTH 

#set company of EPC from virtual reader to the EPC object
EPCtest.setcomp($EPCdequeue,0,13)
puts EPCtest.getcomp()+"\n"
#set item of EPC from virtual reader to the EPC object
EPCtest.setitem($EPCdequeue,0,19)
puts EPCtest.getitem()+"\n"
#set serial of EPC from virtual reader to the EPC object
EPCtest.setserial($EPCdequeue,0,25)
puts EPCtest.getserial()+"\n"
Thread.pass


elsif EPCtest.getbits()==BITS96LENGTH && EPCtest.getpartition()==BITS96PARTITION0 

#set company of EPC from virtual reader to the EPC object
EPCtest.setcomp($EPCdequeue,0,39)
puts EPCtest.getcomp()+"\n"
#set item of EPC from virtual reader to the EPC object
EPCtest.setitem($EPCdequeue,0,3)
puts EPCtest.getitem()+"\n"
#set serial of EPC from virtual reader to the EPC object
EPCtest.setserial($EPCdequeue,0,38)
puts EPCtest.getserial()+"\n"
Thread.pass

elsif EPCtest.getbits()==BITS96LENGTH && EPCtest.getpartition()==BITS96PARTITION1

#set company of EPC from virtual reader to the EPC object
EPCtest.setcomp($EPCdequeue,0,36)
puts EPCtest.getcomp()+"\n"
#set item of EPC from virtual reader to the EPC object
EPCtest.setitem($EPCdequeue,0,6)
puts EPCtest.getitem()+"\n"
#set serial of EPC from virtual reader to the EPC object
EPCtest.setserial($EPCdequeue,0,38)
puts EPCtest.getserial()+"\n"
Thread.pass

else 

#set company of EPC from virtual reader to the EPC object
EPCtest.setcomp($EPCdequeue,0,33)
puts EPCtest.getcomp()+"\n"
#set item of EPC from virtual reader to the EPC object
EPCtest.setitem($EPCdequeue,0,9)
puts EPCtest.getitem()+"\n"
#set serial of EPC from virtual reader to the EPC object
EPCtest.setserial($EPCdequeue,0,38)
puts EPCtest.getserial()+"\n"
Thread.pass

end 

#decrease queue counter by 1
$queueTotal-=1
end

#there is less than 10 EPCs in the queue, stop filtering
puts "There are too few EPCs to do filtering. \n"
}
end
__________________
S.VinothkumaR
Behind me is infinite power,
Before me is Endless Possibility,
Around me is Boundless Opportunity,
Why should I fear!
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP vs Ruby suman PHP Programming 2 10-02-2008 12:59 AM
Is Ruby for Web? S.Vinothkumar Ruby 6 11-20-2007 01:25 AM
How to implement Multithreading in Java? mobilegeek Java Programming 3 09-11-2007 05:08 AM
What is multithreading? Jeyaseelansarc C and C++ Programming 0 05-17-2007 07:26 AM
Ruby IDE drecko Ruby 0 02-16-2007 12:11 AM


All times are GMT -7. The time now is 10:21 AM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.
Our Partners
One Way Moving Companies | Stamford Dentist | Euro Millions Lottery | Home Loans| Furniture

SEO by vBSEO 3.0.0