如何将 std::string 放入 boost::lockfree::queue (或替代)?
我正在尝试将 std::string
s 放入 boost::lockfree::queue
s 中,以便我的线程可以使用新数据相互更新.
I'm trying to put std::string
s into boost::lockfree::queue
s so that my threads can update each other with new data.
当我尝试使用 boost::lockfree::queue<std::string>更新数据;
,g++
说:
When I try to use boost::lockfree::queue<std::string> updated_data;
, g++
says :
在 'class boost::lockfree::queue >' 的实例化中:
In instantiation of 'class boost::lockfree::queue >':
错误:静态断言失败:(boost::has_trivial_destructor::value)
error: static assertion failed: (boost::has_trivial_destructor::value)
错误:静态断言失败:(boost::has_trivial_assign::value)
error: static assertion failed: (boost::has_trivial_assign::value)
我已经 一般显示了什么这些错误意味着,但我没有希望自己解决这个问题,因为我几乎是 C++ 的新手.
I've been shown generally what these errors mean, but I have no hope of ever fixing this myself, as I'm almost brand new to c++.
是否有另一种方法可以使用 lockfree
在线程之间传递文本数据?如果没有,请告诉我如何将 std::string
放入 boost::lockfree::queue
.
Is there an alternative way to pass text data between threads with lockfree
? If not, please show me how to put std::string
into a boost::lockfree::queue
.
推荐答案
如果将原始指针放入队列中,旧的 std::strings
将被泄露,因为无法释放当不再需要它们时.这是因为没有办法在不使用锁的情况下以线程安全的方式释放对象(除了一些技巧,如危险指针,boost::lockfree::queue
不使用)
If you put raw pointers in the queue, the old std::strings
will be leaked, since there is no way to free them when they are no longer needed. This is because there is no way to free the objects in a thread-safe way without taking a lock (other than some tricks like hazard pointers, which boost::lockfree::queue
does not use)
出于我不太了解的技术原因,boost::lockfree::queue
需要一个普通的赋值运算符和一个普通的析构函数,这意味着您的对象不能也不包含任何数据类型必须在其析构函数中释放内存,例如 std::string
.
For technical reasons I do not really understand, the boost::lockfree::queue
requires a trivial assignment operator and a trivial destructor, which means that your object cannot be nor contain any data type that must free memory in its destructor, like std::string
.
相关文章