在另一个线程中使用线程的向量:试图引用已删除的函数时出错
Using a vector of thread in another thread : error attempting to reference a deleted function
我正在尝试发送一个矢量到另一个线程的函数参数:
void foo(){}
const int n = 24;
void Thread_Joiner(std::vector<thread>& t,int threadNumber)
{
//some code
}
int main()
{
std::vector<thread> threads(n, thread(foo));
thread Control_thread1(Thread_Joiner, threads, 0);//error
thread Control_thread2(Thread_Joiner, threads, 1);//error
//...
}
上面的代码给出了这个错误:
: attempting to reference a deleted function
我检查了std::thread
的头文件,似乎复制构造函数被删除了:thread(const thread&) = delete;
std::thread
有一个移动构造函数,但我不认为在这种情况下使用移动是有用的,因为Control_thread1
和Control_thread2
使用相同的vector
!
如果我用thread **threads;...
代替vector
,它工作得很好,但我不想使用指针。
我该怎么办?!
std::thread
复制用于绑定的参数。使用std::ref
包含它作为引用:
std::thread Control_thread1(Thread_Joiner, std::ref(threads), 0);
std::thread Control_thread2(Thread_Joiner, std::ref(threads), 1);
相关文章:
- C++Boost Asio Pool线程,带有lambda函数和传递引用变量
- condition_variable、引用和线程:谁拥有锁?
- 启动类函数作为失去引用的线程
- 将常量引用传递给线程
- 线程安全的引用计数队列C++
- 具有对象的右值引用的线程
- C++11 多线程,通过引用传递
- C++ 线程函数通过引用传递
- C 11线程汇编错误传递字符串作为复制的引用
- C++ 11 您可以通过不同线程中的引用安全地传递和访问 std::atomics 吗?
- 通过引用将变量传递给多个线程会损坏堆
- 线程函数中的通用引用
- 通过引用和多线程传递对象
- 如何在类中进行 c++ 多线程处理(将线程引用保留为成员 var)
- 为线程构造函数传递引用以将其绑定到函数失败
- 通过在引用线程对象来传递取消引用的“this”指针来在函数对象构造函数中创建线程是好是坏
- 对抽象类的引用不能传递给线程函数?
- std::线程传递向量元素(按引用)
- 取消引用指向不同线程中的原子对象的只读非原子指针是否安全
- 引用计数类和多线程