V
Vishnu I.
Hi
I was recently trying to solve a pet problem of mine where threading
seems to be the most natural way to solve a problem. I needed a thread
that would scan a fixed list of tuples each of which contained a single
filenames to sha1 them and add the sha1 to each tuple and a second
thread that would run through this list and contact this service on the
internet to get metadata on these files. (the service exposes a udp api
that allows a given ip to use only a fixed local port and has strict
rate limits, so I don't want to request metadata for files
concurrently). The thing is I have almost never worked worked with
threading since I've mostly built web apps.
So I decided to maintain a list of tuples and a last updated index that
would be visible to both threads.
So something like this
https://gist.github.com/764495#file_simple_multithreaded_example .
However, I understand that this may not work. Because there is no
guarantee of the order in which the two operations are performed and
these might be reordered. But this understanding comes from reading
about this in other languages. Is this also true in Ruby?
I understand that if I were doing this in .net or java I could just make
index volatile which is a directive to the compiler and runtime that
operations surrounding it should not be reordered. Am i correct in
understanding that there is no such concept in ruby? So I cannot do this
locklessly.
So I decided to lock on a mutex and I came up with this.
https://gist.github.com/764495#file_simple_multithreaded_example_with_loc=
ks
now in the scanner thread I have udpated the list inside the sychronized
block and then updated index, but is it safe to move the list updation
outside? (I assume no since regular statements and sych blocks can get
reordered). Also I have sychronized in the updater thread because I dont
know if statements inside sychronized blocks can be re-ordered. Is that
right?
p.s. After I considered this solution, I came to know about the Queue
class=C2=A0in thread, so I know that that is a more elegant solution. But=
I
would still like to know the solution.
-- =
Posted via http://www.ruby-forum.com/.=
I was recently trying to solve a pet problem of mine where threading
seems to be the most natural way to solve a problem. I needed a thread
that would scan a fixed list of tuples each of which contained a single
filenames to sha1 them and add the sha1 to each tuple and a second
thread that would run through this list and contact this service on the
internet to get metadata on these files. (the service exposes a udp api
that allows a given ip to use only a fixed local port and has strict
rate limits, so I don't want to request metadata for files
concurrently). The thing is I have almost never worked worked with
threading since I've mostly built web apps.
So I decided to maintain a list of tuples and a last updated index that
would be visible to both threads.
So something like this
https://gist.github.com/764495#file_simple_multithreaded_example .
However, I understand that this may not work. Because there is no
guarantee of the order in which the two operations are performed and
these might be reordered. But this understanding comes from reading
about this in other languages. Is this also true in Ruby?
I understand that if I were doing this in .net or java I could just make
index volatile which is a directive to the compiler and runtime that
operations surrounding it should not be reordered. Am i correct in
understanding that there is no such concept in ruby? So I cannot do this
locklessly.
So I decided to lock on a mutex and I came up with this.
https://gist.github.com/764495#file_simple_multithreaded_example_with_loc=
ks
now in the scanner thread I have udpated the list inside the sychronized
block and then updated index, but is it safe to move the list updation
outside? (I assume no since regular statements and sych blocks can get
reordered). Also I have sychronized in the updater thread because I dont
know if statements inside sychronized blocks can be re-ordered. Is that
right?
p.s. After I considered this solution, I came to know about the Queue
class=C2=A0in thread, so I know that that is a more elegant solution. But=
I
would still like to know the solution.
-- =
Posted via http://www.ruby-forum.com/.=