954,561 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Ruby multithreading

Hi, I was wondering if there are any Ruby experts out there who can help me. I am currently writing a program and I wish to use multithreading. How do I go about doing this? I have 2 seperate threads (which do different things) created but how do I run them together? Any help would be grateful. Cheers.

jlimty
Newbie Poster
4 posts since Aug 2005
Reputation Points: 10
Solved Threads: 0
 

By "run them together" do you mean end one of the threads? Or do you mean simply to have them run concurrently? I am guessing you already found both of these resources, but here they are anyway:

http://www.rubycentral.com/book/tut_threads.html
http://www.rubycentral.com/ref/ref_c_thread.html

What code do you have right now?

Rashakil Fol
Super Senior Demiposter
Team Colleague
2,658 posts since Jun 2005
Reputation Points: 1,135
Solved Threads: 177
 

By "run them together" do you mean end one of the threads? Or do you mean simply to have them run concurrently? I am guessing you already found both of these resources, but here they are anyway:

http://www.rubycentral.com/book/tut_threads.html http://www.rubycentral.com/ref/ref_c_thread.html

What code do you have right now?

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. Thanks in advance. Cheers. :)

jlimty
Newbie Poster
4 posts since Aug 2005
Reputation Points: 10
Solved Threads: 0
 

So could you show what you have tried?

Rashakil Fol
Super Senior Demiposter
Team Colleague
2,658 posts since Jun 2005
Reputation Points: 1,135
Solved Threads: 177
 

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

def Thread1
Thread.new do
#read input file, check input and queue to a queue
end
end

def Thread2
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?

I've also tried something else recently, which is as follows

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. Thanks heaps. Cheers.

jlimty
Newbie Poster
4 posts since Aug 2005
Reputation Points: 10
Solved Threads: 0
 

The full code would really help show what you're trying to do.

Rashakil Fol
Super Senior Demiposter
Team Colleague
2,658 posts since Jun 2005
Reputation Points: 1,135
Solved Threads: 177
 

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

jlimty
Newbie Poster
4 posts since Aug 2005
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You