henryyao
henryyao

Reputation: 1818

Does exception belong to threads or process in C++?

Let's say we have two running threads that both would throw exceptions and there are exception handlers in these threads. Would C++ be able to handle that, not running into terminated or undefined behavior.

Is it correct that exception belongs to per thread, and each thread can have no more than one exception at a time?

Upvotes: 2

Views: 257

Answers (2)

JaeMann Yeh
JaeMann Yeh

Reputation: 373

The following example shows that the exception handler is using the stack of thread t1 which made a division by zero exception. It means that exception belongs to per thread.

// g++ -std=c++0x -pthread -fnon-call-exceptions main.cpp
#include <iostream>
#include <thread>
#include <signal.h>

void handler(int signo) {
    int handler_local_var;
    std::cout << &handler_local_var << " in stack of handler" << std::endl;
    throw signo;
}

void thread1(std::string msg) {
    int t1_local_var;
    std::cout << &t1_local_var << " in stack of " << msg << std::endl;
    std::this_thread::sleep_for(std::chrono::seconds(2));
    signal(SIGFPE,handler);
    try { 
        int x = 100 / 0; /* ignore warning: division by zero [-Wdiv-by-zero] */
    }
    catch (...) {
        std::cout << "caught" << std::endl;
    }
    while (1) {
        std::this_thread::sleep_for(std::chrono::seconds(2));
    }
}

void thread2(std::string msg) {
    int t2_local_var;
    std::cout << &t2_local_var << " in stack of " << msg <<  std::endl;
    while (1) {
        std::this_thread::sleep_for(std::chrono::seconds(2));
    }
}

int main() {
    int main_local_var;
    std::cout << &main_local_var << " in stack of main" << std::endl;
    std::thread t1(thread1,"t1");
    std::thread t2(thread2,"t2");
    while (1) {
        std::this_thread::sleep_for(std::chrono::seconds(2)); /* Ctrl-C to stop */
    }
    return 0;
}

Test result:

$ ./a.out 
0x7ffee7fea788 in stack of main
0x7f0b54b92d68 in stack of t2
0x7f0b55393d54 in stack of t1
0x7f0b55393754 in stack of handler
caught

Upvotes: 0

Maxim Egorushkin
Maxim Egorushkin

Reputation: 136425

Is it correct that exception belongs to per thread

That is correct.

and each thread can have no more than one exception at a time?

A thread can have more than one active exception. See int uncaught_exceptions() noexcept:

Detects how many exceptions in the current thread have been thrown or rethrown and not yet entered their matching catch clauses.

E.g.:

#include <iostream>
#include <stdexcept>

void f() {
    throw std::runtime_error("error");
}

struct A {
    ~A() {
        std::cout << "uncaught_exceptions: " << std::uncaught_exceptions() << '\n';
    }
};

struct B {
    ~B() {
        try {
            A a;
            f();
        }
        catch(std::exception&) {}
    }
};

int main() {
    try {
        B b;
        f();
    }
    catch(std::exception&) {}
}

Outputs:

uncaught_exceptions: 2

Upvotes: 1

Related Questions