首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >递归调用terminate

递归调用terminate
EN

Stack Overflow用户
提问于 2012-09-10 16:36:53
回答 2查看 25.7K关注 0票数 10

据我所知,当异常处理出现问题时(通常只是没有被捕获),就会调用terminate()。我得到的只是一个错误行terminate called recursively

在用谷歌搜索了一段时间后,我发现了很多例子

代码语言:javascript
运行
复制
terminate called after throwing an instance of ... terminate called recursively

但这不是我的案子。因为我没有关于异常类型的提示,所以我想知道这个terminate called recursively本身意味着什么。

抱歉,我不能提供代码,所以任何猜测都将是有帮助的。我在Ubuntu11.04下用g++ 4.5.2编译。

谢谢你,亚历克斯。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-09-10 16:48:52

可能是某些代码抛出了您没有捕获的异常,这意味着将调用terminate。终止程序意味着可能会调用对象析构函数,如果其中一个中有异常,那么terminate将被“递归”调用。

票数 25
EN

Stack Overflow用户

发布于 2018-11-23 14:41:44

我遇到过这个问题,可能是你在thread poolthread中的函数的error

让我们重新使用terminate called recursively exception

我正在用c++11编写一个线程池,下面是我的代码:

代码语言:javascript
运行
复制
// blocking queue
template<typename T>
class SafeQueue{ 
    public:
        bool pop(T& value){
            std::lock_guard<std::mutex> lock(mtx_);
            if(queue_.empty())
                return false;

            value = queue_.front();
            queue_.pop_front();
            return true;
        }

        void push(T&& value){
            std::lock_guard<std::mutex> lock(mtx_);
            queue_.push_back(std::move(value));
        }

        bool empty(){
            std::lock_guard<std::mutex> lock(mtx_);
            return queue_.empty();
        }

    private:
        std::mutex mtx_;
        std::list<T> queue_;
};

typedef std::function<void()> Task;
typedef SafeQueue<Task> Tasks;

class ThreadPool{
    public:
        ThreadPool(uint32_t nums=5, bool start=false);
        ~ThreadPool();

        void start();
        void addTask(Task&& task);
        void join();
        void exit();
        size_t getThreadNum(){return threadNums_;}

    private:
        Tasks tasks_;

        std::vector<std::thread> threads_;
        size_t threadNums_;
        bool stop_;
};

ThreadPool::ThreadPool(uint32_t nums, bool start):
  threadNums_(nums), stop_(false)
{
    if(start)
        this->start();
}

ThreadPool::~ThreadPool(){
    stop_ = true;
}

void ThreadPool::start(){
    auto lb_thread_fun = [this](){
        while (!stop_){
            Task task;
            tasks_.pop(task);
            // error from here, task maybe empty.
            task();
        }
    };

    for (int i = 0; i < threadNums_; ++i) {
        threads_.push_back(std::thread(lb_thread_fun));
    }
}

void ThreadPool::addTask(Task&& task){
    tasks_.push(std::move(task));
}

void ThreadPool::join(){
    for (auto& th:threads_) {
        th.join();
    }
}

void ThreadPool::exit(){
    stop_ = true;
}

测试代码如下:

代码语言:javascript
运行
复制
#include "my_threadpool.h"
#include <iostream>

using std::cout;
using std::endl;

auto lb_dummy_dw = [](const std::string& url){
    cout<<"start downloading: "<<url<<endl;
    std::this_thread::sleep_for(std::chrono::seconds(2));
    cout<<"downloading success !!!!"<<url<<endl;
};

auto lb_dummy_sql = [](int id, const std::string& name){
    cout<<"start select from db, id:" << id << ", name: "<<name<<endl;
    std::this_thread::sleep_for(std::chrono::seconds(3));
    cout<<"select db success !!!!"<<endl;
};

void test_thread_pool(){
    cout<<"create thread pool with 5 thread"<<endl;
    xy::ThreadPool tp(5);

    cout<<"add 3 * 2 task to thread pool"<<endl;
    for (int i = 0; i < 3; ++i) {
        tp.addTask(std::bind(lb_dummy_dw, "ww.xxx.com"));
        tp.addTask(std::bind(lb_dummy_sql, i, "xy" + std::to_string(i)));
    }

    cout<<"start thread pool"<<endl;
    tp.start();
    tp.join();
}

int main(){
    test_thread_pool();
    return 0;
}

当你运行上面的代码时,你可能会得到下面的输出:

代码语言:javascript
运行
复制
create thread pool with 5 thread
add 3 * 2 task to thread pool
start thread pool
start downloading: ww.xxx.com
start select from db, id:0, name: xy0
start downloading: ww.xxx.com
start select from db, id:1, name: xy1
start downloading: ww.xxx.com
downloading success !!!!ww.xxx.com
start select from db, id:2, name: xy2
downloading success !!!!ww.xxx.com
downloading success !!!!ww.xxx.com
terminate called recursively
terminate called after throwing an instance of 'std::bad_function_call'
  what():  

你可以看到,它得到了terminate called recursively exception. ,因为在start函数中,变量task可能是空的,所以thread pool中的每个线程都抛出了bad_function_call异常。

代码语言:javascript
运行
复制
void ThreadPool::start(){
    auto lb_thread_fun = [this](){
        while (!stop_){
            Task task;
            tasks_.pop(task);
            // error from here, task maybe empty.
            task();
        }
    };

    for (int i = 0; i < threadNums_; ++i) {
        threads_.push_back(std::thread(lb_thread_fun));
    }
}

任务空测试代码如下:

代码语言:javascript
运行
复制
void test_task(){
    Task task;
    try{
        task();
    }catch (std::exception& e){
        cout<<"running task, with exception..."<<e.what()<<endl;
        return;
    }

    cout<<"ending task, without error"<<endl;
}

输出如下:

代码语言:javascript
运行
复制
running task, with exception...bad_function_call
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12347981

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档