Skip to content

Commit 24310b5

Browse files
authored
catch and report exceptions from thread creation
1 parent 611b55f commit 24310b5

File tree

2 files changed

+50
-23
lines changed

2 files changed

+50
-23
lines changed

src/cpucounters.cpp

Lines changed: 42 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3033,6 +3033,8 @@ void enableNMIWatchdog(const bool silent)
30333033
}
30343034
#endif
30353035

3036+
constexpr const char* threadCreateErrorMessage = "This might be due to a too low limit for the number of threads per process. Try to increase it\n";
3037+
30363038
class CoreTaskQueue
30373039
{
30383040
std::queue<std::packaged_task<void()> > wQueue;
@@ -3043,28 +3045,38 @@ class CoreTaskQueue
30433045
CoreTaskQueue(CoreTaskQueue &) = delete;
30443046
CoreTaskQueue & operator = (CoreTaskQueue &) = delete;
30453047
public:
3046-
CoreTaskQueue(int32 core) :
3047-
worker([=]() {
3048+
CoreTaskQueue(int32 core)
3049+
{
30483050
try {
3049-
TemporalThreadAffinity tempThreadAffinity(core, false);
3050-
std::unique_lock<std::mutex> lock(m);
3051-
while (1) {
3052-
while (wQueue.empty()) {
3053-
condVar.wait(lock);
3051+
worker = std::thread([=]() {
3052+
try {
3053+
TemporalThreadAffinity tempThreadAffinity(core, false);
3054+
std::unique_lock<std::mutex> lock(m);
3055+
while (1) {
3056+
while (wQueue.empty()) {
3057+
condVar.wait(lock);
3058+
}
3059+
while (!wQueue.empty()) {
3060+
wQueue.front()();
3061+
wQueue.pop();
3062+
}
3063+
}
30543064
}
3055-
while (!wQueue.empty()) {
3056-
wQueue.front()();
3057-
wQueue.pop();
3065+
catch (const std::exception& e)
3066+
{
3067+
std::cerr << "PCM Error. Exception in CoreTaskQueue worker function: " << e.what() << "\n";
30583068
}
3059-
}
3069+
3070+
});
30603071
}
3061-
catch (const std::exception & e)
3072+
catch (const std::exception& e)
30623073
{
3063-
std::cerr << "PCM Error. Exception in CoreTaskQueue worker function: " << e.what() << "\n";
3074+
std::cerr << "PCM Error: caught exception " << e.what() << " while creating thread for core task queue " << core << "\n" <<
3075+
threadCreateErrorMessage;
3076+
throw; // re-throw
30643077
}
30653078

3066-
})
3067-
{}
3079+
}
30683080
void push(std::packaged_task<void()> & task)
30693081
{
30703082
std::unique_lock<std::mutex> lock(m);
@@ -10939,15 +10951,23 @@ CounterWidthExtender::CounterWidthExtender(AbstractRawCounter * raw_counter_, ui
1093910951
last_raw_value = (*raw_counter)();
1094010952
extended_value = last_raw_value;
1094110953
DBG(3, "Initial Value " , extended_value);
10942-
UpdateThread = new std::thread(
10943-
[&]() {
10944-
while (1)
10945-
{
10946-
MySleepMs(static_cast<int>(this->watchdog_delay_ms));
10947-
/* uint64 dummy = */ this->read();
10954+
try {
10955+
UpdateThread = new std::thread(
10956+
[&]() {
10957+
while (1)
10958+
{
10959+
MySleepMs(static_cast<int>(this->watchdog_delay_ms));
10960+
/* uint64 dummy = */ this->read();
10961+
}
1094810962
}
10963+
);
10964+
}
10965+
catch (const std::exception& e)
10966+
{
10967+
std::cerr << "PCM Error: caught exception " << e.what() << " while creating thread for a CounterWidthExtender\n" <<
10968+
threadCreateErrorMessage;
10969+
throw; // re-throw
1094910970
}
10950-
);
1095110971
}
1095210972
CounterWidthExtender::~CounterWidthExtender()
1095310973
{

src/threadpool.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,14 @@ class ThreadPool {
102102

103103
private:
104104
void addThread() {
105-
threads_.push_back( std::thread( std::bind( &this->execute, this ) ) );
105+
try {
106+
threads_.push_back( std::thread( std::bind( &this->execute, this ) ) );
107+
} catch (const std::exception& e) {
108+
std::cerr << "PCM Error. Exception in ThreadPool::addThread: " << e.what()
109+
<< ". Possible causes: insufficient system resources, thread limit reached, or invalid thread function."
110+
<< " Suggested actions: check system resource availability, verify thread pool configuration, and ensure the thread function is valid.\n";
111+
throw;
112+
}
106113
}
107114

108115
// Executes work items from a std::thread, do not call manually

0 commit comments

Comments
 (0)