私はこの質問に遭遇しました、それは多分error
あなたの関数のthread pool
またはthread
です。
を繰り返しましょう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
throwbad_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