I want to create a thread object in my class/struct but don't want to initialie immediately during declaration. Intead want to do it later. Hence, I did as in below code snippet, I'm getting this compilation error. How do I fix this ?
error C2280: 'std::thread::thread(const std::thread &)' : attempting to reference a deleted function
I observed the line causing this error is :
std::thread t2;
Does it mean that we can't just declare a std::thread without initializing ? This contradicts the answer in this thread Is it possible to define an std::thread and initialize it later?
struct Abc
{
int a;
std::thread t2;
void mem1()
{
printf("mem1\n");
}
void mem2()
{
printf("mem2\n");
std::thread t1(&Abc::mem1, Abc());
t2.swap(t1);
printf("after spawning t1\n");
t2.join();
}
};
Error Details:
C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\xmemory0(611): error C2280: 'std::thread::thread(const std::thread &)' : attempting to reference a deleted function 1>
C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\thread(70) : see declaration of 'std::thread::thread' 1> This diagnostic occurred in the compiler generated function 'Abc::Abc(const Abc &)