views:

156

answers:

5

And what happens when I try to spawn too many?

I'm getting the following error when I spawn more than about 900 threads:

terminate called after throwing an instance of 'dining 1
boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::thread_resource_error>
 >dining 3
'
dining 2
  what():  dining 4
boost::thread_resource_errordining 3

Is this the expected error resulting from attempting to spawn too many threads?

+3  A: 

How many you can spawn depends on the constraints of your operating environment. And yes, boost::thread_resource_error is what you should expect when it cannot get the proper thread resources it needs, per the documentation

Daniel DiPaolo
incidentally, I have a stupid question. Approximately how many it will be in Windows? Around 10? 50? 100? 1000? 10000?
Armen Tsirunyan
It's all very dependent on lots of things. How much memory you have, if there are any operating system-level constraints, etc. The general answer would be "probably at least 1 (but you'd have to check) and maybe lots more"
Daniel DiPaolo
@Daniel: I'll put it another way. Is it realistic to have 10K launched threads in windows in a single process?
Armen Tsirunyan
@Armen - cannot vouch for general case but a test app I worked with topped out on WinXP at < 250 threads. I think 10K would be **a lot of threads**, based on that.
Steve Townsend
+5  A: 

Remember that each thread needs to reserve stack space. That is why there is a limit on the number of threads you can spawn. It looks like you are either hitting this limit or boost is stopping you from hitting this limit.

Here is a link to the most recent boost documentation that documents the behavior you are seeing (the exception thrown): boost thread docs (search for boost::thread_resource_error on that page)

Michael Goldshteyn
A: 

Raymond Chen discusses this issue on Windows on his blog. According to him, using default values for CreateThread, you can typically create about 2000 threads in a single process. So, if you're using Windows, the 900 limit you're running into may be a boost imposed limit, or the parameters used by boost::thread when spawning a new thread are such that you reach the memory limit quicker.

Out of curiosity, are you trying to just figure out the limitations of your operating environment or are you actually trying to write an application that needs so many threads?

Praetorian
A: 

There are operating system limits. It will mostly come down to how much memory you have on your system. If I recall correctly each pthread thread is given a minimum of 32 megabytes of memory. The default is a lot more.

Jay
A: 

You are hitting a hard limit. As others have said there can be two limitations:

  • the number of threads a process can spawn is limited by the OS (either globally or per process)
  • the memory available is limited and each thread reserves its own stack (typically a few MB, and 4 MB * 900 --> 3.6 Go)

Incidentally, this is what is so interesting about Google Go routines. Instead of spawning as much thread as possible, the Go runtime will adapt the number of threads to the number of cores available, and manually multiplex the routines on these physical threads.

Furthermore, the routines are lightweight (reserving only 4 KB each) because they don't use a traditional stack (disappearance of the Stack Overflow !), meaning that you can effectively span a few thousands routines on a typical machine and it won't cost you much.

If you wish to experiment with extreme parallelism:

  • find how to reduce the stack space allocated per thread (beware of Stack Overflow)
  • switch to Go, or find another language implementing routines
Matthieu M.