Boost logo

Boost Users :

From: Yaser Zhian (yahoo_at_[hidden])
Date: 2007-10-22 13:00:47


Apostolos Apostolidis wrote:
> Hi all. I have developed some code allowing a thread group of 500
> threads to run.
> However, as soon as the 381st thread is spawned, I get the following
> runtime error :
>
> terminate called after throwing an instance of
> 'boost::thread_resource_error'
> what(): boost::thread_resource_error
>
> The Boost documentation does not clarify the nature of the error, so I
> was wondering if anyone
> can help me out here. The (simple) program code follows.
>

I can't be sure, but it can be that your program has run out of stack
space to assign to new threads (only one possibility.) You've not
mentioned what platform and compiler you use. I don't know whether it's
possible, but increase the limit of the application stack (should be
among linker options) or set a lower per-thread stack size.

> #include <boost/thread/mutex.hpp>
> #include <boost/thread/thread.hpp>
> #include <boost/bind.hpp>
>
> using namespace std;
>
> boost::mutex io_mutex;
>
> void foo(int i)
> {
> boost::mutex::scoped_lock lock(io_mutex);
> cout << i << endl;
> }
>
> int main(int argc, char *arvg[])
> {
> boost::thread_group tg;
>
> for(int i = 0;i < 500;i++)
> tg.create_thread(boost::bind(&foo,i));
> tg.join_all();
>
> }
>

On the other hand, it is possible in this program you've provided that
the threads created at the beginning terminate before other threads are
created, specially if the "cout" is fast compared to thread creation.
When I tested this on Windows XP, using VC8sp1, no more than a few (3-4)
threads were alive at any given time and the program ended normally.

When I changed the foo() function to:

void foo(int i)
{
   {
     boost::mutex::scoped_lock lock(io_mutex);
     cout << i << endl;
   }
   sleep_relative (1000.0); // Sleep 1000 seconds from now.
}

I could create 2022 additional threads before the program would run out
of resource and throw the exception you mentioned.

-yzt

-- 
"Programming is an art that fights back!"

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net