I'm trying to send a vector to parameter of another thread's 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
//...
}
The above code give this error :
: attempting to reference a deleted function
I checked the header file of std::thread
It seems that copy constructor is deleted : thread(const thread&) = delete;
std::thread
have a move constructor but I don't think in this case using move is helpfull because Control_thread1
and Control_thread2
use same vector
!
If I use thread **threads;...
instead of that vector
It works fine but I don't want to use pointers .
What should I do ?!
std::thread
copies the arguments used for the bind. Use std::ref
to contain it as a reference:
std::thread Control_thread1(Thread_Joiner, std::ref(threads), 0);
std::thread Control_thread2(Thread_Joiner, std::ref(threads), 1);