如何将 std::string 放入 boost::lockfree::queue (或替代方法)?

how to put std::string into boost::lockfree::queue (or alternative)?(如何将 std::string 放入 boost::lockfree::queue (或替代方法)?)

本文介绍了如何将 std::string 放入 boost::lockfree::queue (或替代方法)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 std::string 放入 boost::lockfree::queue 中,以便我的线程可以使用新数据相互更新.

I'm trying to put std::strings into boost::lockfree::queues so that my threads can update each other with new data.

当我尝试使用 boost::lockfree::queue<std::string>updated_data;, 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.

这篇关于如何将 std::string 放入 boost::lockfree::queue (或替代方法)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何将 std::string 放入 boost::lockfree::queue (或替代方法)?

基础教程推荐