Comments on: Ruby Monitor Basics, or, How the heck do I synchronize producers/consumers https://blog.maxaller.name/2010/10/ruby-monitor-basics-or-how-the-heck-do-i-synchronize-producersconsumers/ ruby, ubuntu, etc Thu, 17 Mar 2016 20:35:40 +0000 hourly 1 http://wordpress.org/?v=3.5.1 By: Delicious Bookmarks for November 12th from 21:16 to 21:56 « Lâmôlabs https://blog.maxaller.name/2010/10/ruby-monitor-basics-or-how-the-heck-do-i-synchronize-producersconsumers/comment-page-1/#comment-468 Delicious Bookmarks for November 12th from 21:16 to 21:56 « Lâmôlabs Sat, 13 Nov 2010 03:02:51 +0000 https://blog.maxaller.name/?p=376#comment-468 [...] Ruby Monitor Basics, or, How the heck do I synchronize producers/consumers « occasionally usef… – November 12th ( tags: ruby threads monitor synchronization consumer producers queues synchronize thread ) [...]

]]>
By: Max https://blog.maxaller.name/2010/10/ruby-monitor-basics-or-how-the-heck-do-i-synchronize-producersconsumers/comment-page-1/#comment-443 Max Sun, 31 Oct 2010 22:32:40 +0000 https://blog.maxaller.name/?p=376#comment-443 That’s true, but Ruby’s (green) threads are comparatively cheap to native threads; which is why JRuby has an option to use a threadpool, built into the interpreter itself (so Thread.new won’t necessarily create a new thread).

Anyway, for ReReplay I was initially using EventMachine, but I needed events to happen at very particular points in the future, and EventMachine’s granularity (which is at best 10ms) simply wasn’t good enough. Using threads I can have a granularity of under a millisecond. I was just sharing some commentary regarding what I learned while using monitors.

]]>
By: Lawrence Pit https://blog.maxaller.name/2010/10/ruby-monitor-basics-or-how-the-heck-do-i-synchronize-producersconsumers/comment-page-1/#comment-442 Lawrence Pit Sun, 31 Oct 2010 22:06:42 +0000 https://blog.maxaller.name/?p=376#comment-442 If expensiveness of threads is a worry to you, then they’re out. Use EventMachine, Revactor or Fibers instead.

]]>
By: Max https://blog.maxaller.name/2010/10/ruby-monitor-basics-or-how-the-heck-do-i-synchronize-producersconsumers/comment-page-1/#comment-440 Max Sun, 31 Oct 2010 18:14:38 +0000 https://blog.maxaller.name/?p=376#comment-440 Okay, so here’s what I think you’re suggesting: http://gist.github.com/656925

So you still need two condition variables, even with only one monitor. The example is a bit more complicated now, and…I’m not sure exactly what the benefit of subclassing Thread is. But there you have it.

]]>
By: Max https://blog.maxaller.name/2010/10/ruby-monitor-basics-or-how-the-heck-do-i-synchronize-producersconsumers/comment-page-1/#comment-439 Max Sun, 31 Oct 2010 15:49:44 +0000 https://blog.maxaller.name/?p=376#comment-439 So, I don’t actually care about what the outputs are, only that the job has finished. The reason there are two monitors here is because the action of popping off jobs and completing them are completely independent — there’s no reason one thread couldn’t be decrementing the results_unprocessed counter while another is simultaneously popping off a new job.

I’m not sure I really like a solution that involves iterating over a list every time we want to see if we’re done — if you have a lot of worker threads (50? 100?) that’s a lot of unnecessary time spent inside the monitor (O(n) time on number of threads instead of O(1)).

Lastly, I disagree that adding new jobs during execution wouldn’t be easy — it would be as simple as adding the job to the queue and incrementing the results_unprocessed variable. (Not sure if the queue manipulation would need to be inside the q synchronize block, but whatever).

In any case, I’ll write up your idea and include it here, see if it makes more sense to me.

]]>
By: lucasdicioccio https://blog.maxaller.name/2010/10/ruby-monitor-basics-or-how-the-heck-do-i-synchronize-producersconsumers/comment-page-1/#comment-437 lucasdicioccio Sun, 31 Oct 2010 14:25:52 +0000 https://blog.maxaller.name/?p=376#comment-437 If you use queues in an example, you could just use two of them for your goal: one to add jobs, the second to wait for their results (elegant solution when you have as many IN events than OUT events).

The monitor construct is useful in a single case: when you want to make sure that at most one thread execute inside the monitor. Usually all threads wait for a condition before entering the monitor, does stuff, exits (or wait again), and signal.

To demonstrate it better, I think that you should have implemented the queue within the monitor as well because here you’re pop-ing with a synchronization construct, and monitoring the queue size in another one (without possibility to add new jobs in the queue easily).

I would have done everything with the monitor construct with:
- a Worker subclass of Thread with a busy? method
- a regular array joblist that you can test on empty?

Basically, you exit when joblist.empty? and not workers.find(&:busy?) which is easier to understand that test on a variable tracking the queue length.

]]>