线程安全队列出现分段错误

Thread safe queue gives segmentation fault

本文关键字:分段 错误 安全 队列 线程      更新时间:2023-10-16

我正在编写一个线程安全队列来添加字符串流对象。我以为发生了分段错误,因为stringstream ss添加到队列时被破坏了,但正如您所看到的,我正在移动它std::move()

#include <iostream>
#include <mutex>
#include <sstream>
#include <queue>
#include <condition_variable>
template <typename T>
class ThreadSafeQueue
{
public:
/* 
Returns the front element and removes it from the collection
No exception is ever returned as we garanty that the deque is not empty
before trying to return data.
This is useful in our while loop renderer, because it just waits if there
are no members to be popped.
*/
T pop(void) noexcept
{
std::unique_lock<std::mutex> lock{_mutex};
while (_collection.empty())
{
_condNewData.wait(lock);
}
auto elem = std::move(_collection.front());
_collection.pop();
return elem;
}
template <typename... Args>
void emplace(Args &&... args)
{
addDataProtected([&] {
_collection.emplace(std::forward<Args>(args)...);
});
}
private:
/*
Locks the thread and do something with the deque. 
Then unique_lock goes away and unlocks the thread
*/
template <class F>
decltype(auto) lockAndDo(F &&fct)
{
std::unique_lock<std::mutex> lock{_mutex};
return fct();
}
template <class F>
void addDataProtected(F &&fct)
{
lockAndDo(std::forward<F>(fct));
_condNewData.notify_one();
}
private:
std::queue<T> _collection;            // Concrete, not thread safe, storage.
std::mutex _mutex;                    // Mutex protecting the concrete storage
std::condition_variable _condNewData; // Condition used to notify that new data are available.
};
int main()
{
std::unique_ptr<ThreadSafeQueue<std::stringstream>> logMessages;
std::stringstream ss;
ss << "hello";
logMessages->emplace(std::move(ss));
return 0;
}
std::unique_ptr<ThreadSafeQueue<std::stringstream>> logMessages;

您从未为此变量分配内存。 它不指向ThreadSafeQueue<>对象

auto logMessages = std::make_unique<ThreadSafeQueue<std::stringstream>>();

相反?

或者只是按照评论中的建议制作普通对象。