Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How many threads can I spawn using boost in c++?

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?

like image 501
jonderry Avatar asked Oct 14 '10 20:10

jonderry


3 Answers

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)

like image 129
Michael Goldshteyn Avatar answered Nov 17 '22 23:11

Michael Goldshteyn


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

like image 4
Daniel DiPaolo Avatar answered Nov 17 '22 23:11

Daniel DiPaolo


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
like image 1
Matthieu M. Avatar answered Nov 17 '22 23:11

Matthieu M.