terminate递归调用
terminate called recursively
据我所知,当异常处理出现问题时(通常只是没有捕获到),就会调用terminate()
。我得到的只是一个错误行terminate called recursively
。
在谷歌上搜索了一段时间后,我发现了很多的例子
terminate called after throwing an instance of ... terminate called recursively
但这不是我的情况。由于我没有关于异常类型的提示,我想知道这个terminate called recursively
本身意味着什么。
很抱歉,我无法提供代码,所以任何猜测都会有所帮助。我正在Ubuntu 11.04下使用g++4.5.2进行编译。
非常感谢,亚历克斯。
可能是某些代码抛出了一个您没有捕捉到的异常,这意味着terminate
将被调用。终止程序意味着可能会调用对象析构函数,如果其中一个析构函数中存在异常,则terminate
将被"递归"调用。
我遇到过这个问题,可能是thread pool
或thread
中函数的error
。
让我们重复terminate called recursively
exception
。
我正在用c++11
编写一个线程池,这是我的代码:
// 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;
}
测试代码如下:
#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;
}
当您运行上述代码时,您可能会得到以下输出:
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
异常因为在函数start
中,变量task
可能为空,所以thread pool
中的每个线程都会抛出bad_function_call
异常。
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 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;
}
输出如下:
running task, with exception...bad_function_call
相关文章:
- 如何在Elixir中调用递归函数并行
- 返回递归调用和仅递归调用的区别
- 使用静态变量的递归调用的不同输出
- 即使没有调用这个递归函数,它是如何工作的?
- 在对象指针上调用 Delete 是否会递归删除其动态分配的成员
- 复制构造函数中的递归调用
- 为什么编译器将其解析为函数指针而不是递归调用?
- 如何在 c++ 的类中递归调用函数方法?
- 如何在递归函数调用中返回当前函数值
- 了解使用堆栈实现队列的递归调用机制
- 递归函数调用在后台工作
- 如何通过函数指针递归调用类成员函数?
- 条件加倍时的递归调用
- 对可变参数函数的递归调用的链接器错误
- 在循环中调用递归函数
- ambigus调用递归sudoku_backtracker函数
- 如何在C++中调用递归链表遍历函数
- 为什么在NULL检查之后要在指针上调用递归函数?
- 通过结构调用递归函数
- 如果在指令之前调用递归函数怎么办