Tôi nhận được một ++ lỗi C với luồng:C++ chấm dứt gọi mà không có một ngoại lệ hoạt động
terminate called without an active exception
Aborted
Đây là mã:
#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;
};
tôi làm mẫu mã của tôi xung quanh ví dụ này: http://www.justsoftwaresolutions.co.uk/threading/implementing-a-thread-safe-queue-using-condition-variables.html
Tôi đang làm gì sai và cách khắc phục lỗi?
Bạn có 'tham gia' tất cả các chủ đề của bạn trong chương trình chính không? –
Hiển thị cho chúng tôi phần còn lại của mã. – Matt
@Kerrek ah ha điều này đã khắc phục được sự cố, tôi không biết tại sao dù tôi chắc chắn rằng chủ đề chính vẫn chưa chấm dứt trước khi công nhân kết thúc. Cũng làm alogorithms khóa của tôi nhìn phải không? – 111111