Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
124 views
in Technique[技术] by (71.8m points)

c++ - what does boost::thread::interrupt do?

Following code is printing Inside Thread function only once

#include <iostream>
#include <boost/thread.hpp>
using namespace std;


void thread_fn() {
    while (true) {
        std::cout << "Inside thread function
";
        boost::this_thread::sleep_for(boost::chrono::seconds(1000));
    }
}

int main() {
    boost::thread bt(thread_fn);
    while (true) {
        boost::this_thread::sleep_for(boost::chrono::seconds(1));
        bt.interrupt();
    }
    bt.join();
    return 0;
}

but It is working fine with following function

void thread_fn() {
    while (true) {
        try {
            std::cout << "Inside thread function
";
            boost::this_thread::sleep_for(boost::chrono::seconds(1000));
        } catch (boost::thread_interrupted&) {
            std::cout << "Interrupt received" << std::endl;
        }
    }
}

Official documentations says following line

When the interrupted thread next executes one of the specified interruption points (or if it is currently blocked whilst executing one) with interruption enabled, then a boost::thread_interrupted exception will be thrown in the interrupted thread. If not caught, this will cause the execution of the interrupted thread to terminate. As with any other exception, the stack will be unwound, and destructors for objects of automatic storage duration will be executed.

If un-unhandled exception is causing the execution of the interrupted thread to terminate, why program is not crashing and coming out of following loop in main()

while (true) {
        boost::this_thread::sleep_for(boost::chrono::seconds(1));
        bt.interrupt();
    }
question from:https://stackoverflow.com/questions/65852355/what-does-boostthreadinterrupt-do

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If un-unhandled exception is causing the execution of the interrupted thread to terminate, why program is not crashing and coming out of following loop in main()

Simply because that's how Boost.Thread was designed to operate.

They've made it so that interrupt() will throw an exception, and failing to catch the exception terminates the thread (but not the process).

Nothing in the documentation, either that which you've quoted, or that which can be found elsewhere, says that your program should be expected to crash.


From the documentation:

A running thread can be interrupted by invoking the interrupt() member function of the corresponding boost::thread object. When the interrupted thread next executes one of the specified interruption points (or if it is currently blocked whilst executing one) with interruption enabled, then a boost::thread_interrupted exception will be thrown in the interrupted thread. If not caught, this will cause the execution of the interrupted thread to terminate.

and:

If the function or callable object passed to the boost::thread constructor propagates an exception when invoked that is not of type boost::thread_interrupted, std::terminate() is called.

Presumably they just catch that kind of exception in the code that invokes your thread entrypoint, and return safely, ending the thread. Thus, although you didn't handle the exception, it's not an unhandled exception as far as C++ is concerned.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...