0

Possible Duplicate:
Ruby sleep or delay less than a second?

I have 100-1000 Threads running (or more). Each of those threads should execute a specific method at the exact same moment (most exact as possible).

The only solution I can think of is having each thread sleeping for the difference to a certain Timestamp, however sleep() is not that accurate. I was also thinking about using EventMachine and EventMachine::Timer instead, however this seems to be even less reliable and accurate.

What technique would you use to achieve the best results?

Community
  • 1
  • 1
John Meyer
  • 27
  • 1
  • 3
  • It's customary on Stack Overflow to show an example of the code you've written to solve the problem. That way we can help you fix your code, not write code for you. As is, your question could run afoul of the "not constructive" or "not a real question" censors. – the Tin Man Oct 27 '12 at 18:08
  • Also specify the version of Ruby and threading model used. There is a difference between Green threads and Native threads. Although, ultimately the timing and accuracy (or lack thereof) comes down to the scheduler and load .. –  Oct 27 '12 at 18:12
  • Are threads, perhaps not the ideal approach here? –  Oct 27 '12 at 18:17
  • To clarify, this is not a duplicate of the thread that our police officer "the Tin Man" suspected. I don't have any code yet ready to show you. My problem is that I need to schedule events at the same time, and I'm thinking of the best solution before I start writing code - this is why I started the thread. I don't expect anybody to write code for me, I'm trying to find out what the best approach to this is. – John Meyer Oct 27 '12 at 18:28
  • I'm open to any Ruby version regarding this. I'm trying to find the best possible solution to solve the problem described. – John Meyer Oct 27 '12 at 18:30
  • 4
    You can not do what you're attempting with ruby. At least if high accuracy is required. Ruby is not suited for real-time applications and does not have true concurrency with threads (there is a GIL, Global Interpreter Lock, preventing multiple simultaneous threads all running ruby code at the same time). – Casper Oct 27 '12 at 18:38
  • @Casper I thought "GIL" was a Python construct (I thought Ruby used Green threads w/o a GIL, although I don't know how the latest/JVM implementations work).. in any case, I agree about the other bit - Ruby (or rather, Ruby run-times) is not designed for real-time nor do they run in a real-time environment/OS. –  Oct 27 '12 at 18:50
  • @pst - No Ruby has a GIL. JRuby works a bit differently, but it's still a stretch for any interpreted language to run 1000 threads concurrently with high accuracy. But especially with the GIL, it's not possible: http://nairrohit.wordpress.com/2011/08/20/concurrency-in-ruby/ – Casper Oct 27 '12 at 19:02
  • 1
    Just wanted to mention that to run one method the "same" time from 1000 threads requires 1000 hardware threads!!! – zakkak Oct 27 '12 at 19:24

1 Answers1

5

A lot of people don't know that sleep takes a float value:

Suspends the current thread for duration seconds (which may be
any number, including a Float with fractional seconds). Returns the actual
number of seconds slept (rounded), which may be less than that asked for if
another thread calls Thread#run. Called without an argument, sleep() will
sleep forever.

  Time.new    #=> 2008-03-08 19:56:19 +0900
  sleep 1.2   #=> 1
  Time.new    #=> 2008-03-08 19:56:20 +0900
  sleep 1.9   #=> 2
  Time.new    #=> 2008-03-08 19:56:22 +0900

There isn't a guarantee the delay will be exact:

3.times do 
  t1 = Time.now.to_f
  sleep 0.5
  t2 = Time.now.to_f
  puts t2 - t1
end

Results in:

0.501162052154541
0.5010881423950195
0.5001001358032227

Additional tasks running will probably make that skew more.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
  • This addressees the OPs statement about sleep not being that accurate - sleep is accurate within the constraints of the system load and time slices given to Ruby. *I* am very aware of the issues of trying to schedule 100 events at the same instant in time. – the Tin Man Oct 27 '12 at 18:18
  • 1
    Hey the Tin Man, are you some kind of police officer here? I appreciate that you make me familiar with StackOverflow's etiquettes, but if you spend more time on doing that than on actually helping me then I think there is something wrong with you. – John Meyer Oct 27 '12 at 18:25
  • Excuse me? I think I've made two different attempts to help you. I'm trying to help your question get answers because it was a target for closing, and, I provided some additional information that should help you, or future people looking for information. – the Tin Man Oct 27 '12 at 18:34
  • @theTinMan No, it doesn't address the question. The edits *do* verify what was stated in the question .. "however sleep() is not that accurate". The first/original post is *just misleading*. I have given a +1 for the edits as they do say "hey, sleep truly isn't accurate", even though it does not provide an alternative. (And this might be the best answer - e.g. look for a different approach entirely - but it should be drawn out as the focus and not a back-burner idea not well-tied to the original question.) –  Oct 27 '12 at 18:46
  • 2
    I'm not sure that Ruby is capable, at least MRI and its current state, of providing the sort of accuracy needed. *Maybe* JRuby with its Java underpinnings could do a little better, but 1000+ threads or processes, firing at the same instant seems like a steep request even in C, especially on commodity hardware like most of us get to use these days. – the Tin Man Oct 27 '12 at 18:58