I am using gcc v 4.1.3 on ubuntu 7.10 with 512MB of RAM. Memory is not a
problem, since I checked
and verified that the thread group does not consume more memory than
available. Also, the maximum
number of threads defined by the system is 8055, so no problem with that
also. Any ideas?
On 22/10/2007, Yaser Zhian
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
#include #include 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 mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users