Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't Ruby have a ThreadPool built-in?

I have a program that creates 10000 threads at once, and runs 8 at the same time.

But ruby doesn't have a ThreadPool built-in as Java. Is there a good reason?

like image 600
Cheng Avatar asked Oct 01 '10 15:10

Cheng


2 Answers

probably because it's easy to roll your own using the standard library "Queue" class.

q = Queue.new
3.times { Thread.new {  while something = q.pop(true) rescue nil; ... }

It's a good question though--I might suggest bringing it up with Ruby Core.

like image 59
rogerdpack Avatar answered Sep 29 '22 01:09

rogerdpack


My suspicion would be it's because a ThreadPool wouldn't be that useful in C-based implementations of Ruby. You can use only one processor at a time with Matz's Ruby Intepreter or Yet Another Ruby VM.

If you want multiple threads to be run on multiple processors, you need to use JRuby instead.

like image 33
Andrew Grimm Avatar answered Sep 28 '22 23:09

Andrew Grimm