C ++终止调用,没有活动异常 [英] C++ terminate called without an active exception
问题描述
我在线程中遇到C ++错误:
I am getting a C++ error with threading:
terminate called without an active exception
Aborted
以下是代码:
#include <queue>
#include <thread>
#include <mutex>
#include <condition_variable>
template<typename TYPE>
class blocking_stream
{
public:
blocking_stream(size_t max_buffer_size_)
: max_buffer_size(max_buffer_size_)
{
}
//PUSH data into the buffer
blocking_stream &operator<<(TYPE &other)
{
std::unique_lock<std::mutex> mtx_lock(mtx);
while(buffer.size()>=max_buffer_size)
stop_if_full.wait(mtx_lock);
buffer.push(std::move(other));
mtx_lock.unlock();
stop_if_empty.notify_one();
return *this;
}
//POP data out of the buffer
blocking_stream &operator>>(TYPE &other)
{
std::unique_lock<std::mutex> mtx_lock(mtx);
while(buffer.empty())
stop_if_empty.wait(mtx_lock);
other.swap(buffer.front());
buffer.pop();
mtx_lock.unlock();
stop_if_full.notify_one();
return *this;
}
private:
size_t max_buffer_size;
std::queue<TYPE> buffer;
std::mutex mtx;
std::condition_variable stop_if_empty,
stop_if_full;
bool eof;
};
我在这个例子中建立了我的代码:
http://www.justsoftwaresolutions.co.uk/threading/implementing-a-thread-safe -queue-using-condition-variables.html
I modeled my code around this example: http://www.justsoftwaresolutions.co.uk/threading/implementing-a-thread-safe-queue-using-condition-variables.html
我做错了什么,如何解决错误?
What am I doing wrong and how do I fix the error?
推荐答案
当线程对象超出范围并且处于可连接状态时,程序终止。标准委员会有一个可连接线程的析构函数的另外两个选项。它可以静静地加入 - 但加入可能永远不会返回,如果线程卡住。或者它可以分离线程(分离的线程不可连接)。然而,分离的线程是非常棘手的,因为它们可能生存直到程序结束和搞乱资源的释放。所以如果你不想终止你的程序,请确保你加入(或分离)每个线程。
When a thread object goes out of scope and it is in joinable state, the program is terminated. The Standard Committee had two other options for the destructor of a joinable thread. It could quietly join -- but join might never return if the thread is stuck. Or it could detach the thread (a detached thread is not joinable). However, detached threads are very tricky, since they might survive till the end of the program and mess up the release of resources. So if you don't want to terminate your program, make sure you join (or detach) every thread.
这篇关于C ++终止调用,没有活动异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!