From 5ee96032c9f918d03365c13c939a7ba0d1efaa2c Mon Sep 17 00:00:00 2001 From: Horst Schirmeier Date: Wed, 19 Feb 2014 18:53:18 +0100 Subject: [PATCH] jobserver: gracefully handle thread creation failures Due to the previous DatabaseCampaign fix, this may not be necessary anymore, but it's nevertheless a good idea to handle thread creation failures properly. Change-Id: I8317a77dd5338509727e737040944320e7755ae3 --- src/core/cpn/JobServer.cc | 36 ++++++++++++++++++++++++------------ 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/src/core/cpn/JobServer.cc b/src/core/cpn/JobServer.cc index dc7b7913..88b94765 100644 --- a/src/core/cpn/JobServer.cc +++ b/src/core/cpn/JobServer.cc @@ -171,18 +171,30 @@ void JobServer::run() continue; } } - // Spawn a thread for further communication, - // and add this thread to a list threads - // We can limit the generation of threads here. - if (m_threadlist.size() >= m_maxThreads) { - // Run over list with a timed_join, - // removing finished threads. - do { - m_threadlist.remove_if(timed_join_successful(m_threadtimeout)); - } while (m_threadlist.size() == m_maxThreads); - } - // Start new thread - th = new boost::thread(CommThread(cs, *this)); + + bool creation_failed = false; + do { + // Spawn a thread for further communication, + // and add this thread to a list threads + // We can limit the generation of threads here. + if (m_threadlist.size() >= m_maxThreads || creation_failed) { + // Run over list with a timed_join, + // removing finished threads. + do { + m_threadlist.remove_if(timed_join_successful(m_threadtimeout)); + } while (m_threadlist.size() >= m_maxThreads); + } + // Start new thread + try { + th = new boost::thread(CommThread(cs, *this)); + creation_failed = false; + } catch (boost::thread_resource_error e) { + cout << "failed to spawn thread, throttling ..." << endl; + creation_failed = true; + sleep(1); + } + } while (creation_failed); + m_threadlist.push_back(th); } close(s);