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?...
| |||||||
| Register | FAQ | Members List | Calendar | Mark Forums Read |
|
#1
| |||
| |||
| 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! |
|
#2
| |||
| |||
| 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! |
|
#3
| |||
| |||
| By "run them together" do you mean end one of the threads? Or do you mean simply to have them run concurrently? |
|
#4
| |||
| |||
| 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! |
|
#5
| |||
| |||
| Oh..fine…Can u do one thing? Can u post here what you have tried? |
|
#6
| |||
| |||
| 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 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
__________________ S.VinothkumaR Behind me is infinite power, Before me is Endless Possibility, Around me is Boundless Opportunity, Why should I fear! |
|
#7
| |||
| |||
| 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
__________________ S.VinothkumaR Behind me is infinite power, Before me is Endless Possibility, Around me is Boundless Opportunity, Why should I fear! |
|
#8
| |||
| |||
| Wow..fine… If u post the full code that will help to find the solution…can u post ur full code here? |
|
#9
| |||
| |||
| 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! |
![]() |
| Thread Tools | |
| Display Modes | |
| |
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 |
Our Partners |