Question

I'm playing with Thread and I discovered that I cannot run 10000 threads.

It gives me the following error:

        threading.rb:23:in `initialize': can't create Thread (35) (ThreadError)
        from threading.rb:23:in `new'
        from threading.rb:23:in `block in <main>'
        from threading.rb:22:in `times'
        from threading.rb:22:in `<main>'

Then I tried to see what's the maximum number and when I make up to 2046 threads, Ruby will run the code.

Why 2046? It appears to follow a pattern of memory like 512, 1024, 2046...

The threading.rb code:

    threads = []
    counter = 1000

    ARGV.each do |a|
      counter = a.to_i
    end

    lines = 0

    counter.times do |i|
      puts "This is index number #{i}."
    end

    puts "You've just seen the normal printing and serial programming.\n\n"

    counter.times do |i|
      Thread.new do
        some_number = Random.rand(counter)
        sleep 1
        puts "I'm thread number #{i}. My random number is #{some_number}.\n"
        lines += 1
      end
    end

    messaged = false
    while lines < counter
      puts "\nWaiting to finish.\n" unless messaged
      print '.'
      puts "\n" if lines == counter
      messaged = true
    end

    puts "\nI've printed #{lines} lines.\n"
    puts "This is end of the program."
Was it helpful?

Solution

OS X limits the number of threads a process can spawn to 2046. This applies to Ruby as well.

Source & full explanation: "How many threads is too many?"

On OSX, there's a hard limit on the number of threads that one process can spawn. On recent versions, that limit is somewhere around 2000 threads.

...

If I run this same bit of code on a Linux machine, I'm able to spawn 10,000 threads without blinking. So, it's possible to spawn a lot of threads, but you probably don't want to.

Here's a test program you can run to see the limit.

1.upto(10_000) do |i|
  Thread.new { sleep }
  puts i
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top