Okay, lets say I have a list of objects. Each object can perform an action every five seconds, the time it takes to perform an action is random and might very well be more than five seconds.
How do I keep all the objects performing their actions as soon as possible in order to get the best throughput? Threads, I suppose, but what should I store the objects in, and how do I check when one is done.
Perhaps if I put a mutex (I hope that's the right name) in each object, along with a timestamp. A priority queue? If so, do I sort on the timestamps? I guess I do, thanks for the help!
Name:
Anonymous2006-11-05 10:59
Alright, this is my algorithm. Can I improve it? I do the mutex locking, unlocking inside the spam function, should I instead use the synchronize function in the Mutex class? while true
pq.each do |p|
if Time.now > (pq.first.priority + 5) && !pq.first.mutex.locked?
pq.first.priority = Time.now
Thread.start do
spam pq.first
end
break
end
end
pq.sort!
end
I suppose this is both faster and better. http://dis.4chan.org/read/lang/1162543556/551,1000 while true
pq.each do |p|
if Time.now > (p.priority + 5) && !p.mutex.locked?
p.priority += 20
Thread.start do
p.mutex.synchronize do
spam p
end
end
break
end
end
pq.sort!
end
Name:
Anonymous2006-11-05 11:22
And if I did not already say so, I change p.priority in spam. I guess I should rather do it in the Thread block, as it doesn't really belong in the spam function.:wq
Bringing /prog/ back to its people
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy