具有对象的右值引用的线程
thread with rvalue reference of an object
我有一个函数,它接受对象的右值引用,我想在std::thread
中运行这个函数。以下代码段
#include <iostream>
#include <thread>
class MyType {
public:
explicit MyType(int m) : myint_(m) {}
MyType(const MyType& ) = delete;
MyType ( MyType&& ) = delete;
MyType operator = (const MyType& ) = delete;
MyType operator = (const MyType&&) = delete;
private:
int myint_;
};
void Run(const MyType&& t) {
// do somthing with t.
}
int main()
{
MyType m{100};
std::thread t(Run, std::move(m));
t.join();
return 0;
}
我已经删除了默认的移动和复制构造函数。就我而言,可以定义一个默认的移动构造函数,但我不希望有一个复制构造函数sizeof(MyType)
,因为它可能很大,而且我担心调用复制构造函数时的内存。
我需要有关如何实现这一目标的建议。
问候。
多亏了@SamVarshavchik,我现在让它与std::unique_ptr
一起工作
#include <iostream>
#include <thread>
class MyType {
public:
explicit MyType(int m) : myint_(m) {}
MyType(const MyType& ) = delete;
MyType ( MyType&& ) = delete;
MyType operator = (const MyType& ) = delete;
MyType operator = (const MyType&&) = delete;
private:
int myint_;
};
void Run(std::unique_ptr<MyType>&& t) {
// do somthing with t.
}
int main()
{
auto m = std::make_unique<MyType>(100);
std::thread t(Run, std::move(m));
t.join();
return 0;
}
后续问题:unique_ptr不可复制,只能移动。它如何与它一起使用,但不适用于我的数据类型(即使我使其可移动(?
发生的事情是std::thread
将其参数复制到类似元组的东西中,然后在线程中使用这些参数调用函数。
在特定情况下,线程的寿命不会超过创建std::thread
对象的范围。 这不是典型的。
获得您想要工作的最简单方法是:
MyType m{100};
std::thread t([&]{ Run(std::move(m)); });
t.join();
您传入 lambda 并自己管理复制和生命周期的地方。
请注意,如果此线程的寿命超过创建m
的范围,则将遵循悬而未决的引用。
std::thread t([pm=std::make_unique<MyType>(100)]() mutable { Run(std::move(*pm)); });
是一个版本,我直接在堆上创建对象,然后使用智能指针随线程携带它。 正确编写的std::thread
应该只需要其可调用的可移动(编写不正确的可调用将它存储在 std 函数中可能会在此处生成构建错误;您可以将唯一的 ptr 换成共享的 PTR,以获得适度的额外开销,但让它在不符合标准的 std 线程上编译(。
我的一般经验法则是不使用基于 INVOKE 的线程帮助程序,而是使用无参数 lambda 并处理参数存储和传递自己。
线程 API 来自 C++ 年前 lambda 时代的 boost;lambda 是你真正应该掌握的东西,并且可以取代在基于 INVOKE 的 API 的怪癖方面获得专业知识(即使知道 INVOKE 是什么,你也不需要知道!
(std::invoke
和std::apply
,OTOH,很棒,即使它们是调用的;我的主要抱怨是违反了SRP(单一责任原则(,我们将线程结构与调用结构混合在一起。
- C++Boost Asio Pool线程,带有lambda函数和传递引用变量
- condition_variable、引用和线程:谁拥有锁?
- 启动类函数作为失去引用的线程
- 将常量引用传递给线程
- 线程安全的引用计数队列C++
- 具有对象的右值引用的线程
- C++11 多线程,通过引用传递
- C++ 线程函数通过引用传递
- C 11线程汇编错误传递字符串作为复制的引用
- C++ 11 您可以通过不同线程中的引用安全地传递和访问 std::atomics 吗?
- 通过引用将变量传递给多个线程会损坏堆
- 线程函数中的通用引用
- 通过引用和多线程传递对象
- 如何在类中进行 c++ 多线程处理(将线程引用保留为成员 var)
- 为线程构造函数传递引用以将其绑定到函数失败
- 通过在引用线程对象来传递取消引用的“this”指针来在函数对象构造函数中创建线程是好是坏
- 对抽象类的引用不能传递给线程函数?
- std::线程传递向量元素(按引用)
- 取消引用指向不同线程中的原子对象的只读非原子指针是否安全
- 引用计数类和多线程