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

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

问题描述

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

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>更新数据;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 (或替代)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆