Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to define an std::thread and initialize it later?

Tags:

My aim is to keep an std::thread object as data member, and initialize it when needed.
I'm not able to do this (as in my code below) because the copy constructor of the std::thread class is deleted. Is there any other way to do it?

class MyClass {     public:         MyClass():DiskJobThread(){};         ~MyClass();          void DoDiskJobThread();      private:         int CopyThread(const std::wstring & Source, const std::wstring & Target);         int MoveThread(const std::wstring & Source, const std::wstring & Target);         std::thread DiskJobThread; };  MyClass::~MyClass() {     DiskJobThread.join(); }  void MyClass::DoDiskJobThread() {     std::wstring Source = GetSource();     std::wstring Target = GetTarget();     int m_OperationType = GetOperationType();     if      (m_OperationType == OPERATION_COPY)     {         DiskJobThread = std::thread(&MyClass::CopyThread, *this, Source, Target);     }     else if (m_OperationType == OPERATION_MOVE)     {         DiskJobThread = std::thread(&MyClass::MoveThread, *this, Source, Target);     } } 
like image 653
hkBattousai Avatar asked Aug 22 '13 09:08

hkBattousai


People also ask

How do you initialize a thread in C++?

To start a thread we simply need to create a new thread object and pass the executing code to be called (i.e, a callable object) into the constructor of the object. Once the object is created a new thread is launched which will execute the code specified in callable. After defining callable, pass it to the constructor.

Is std :: thread copyable?

std::thread::operator= thread objects cannot be copied (2).

Can a thread start another thread C++?

A thread does not operate within another thread. They are independent streams of execution within the same process and their coexistence is flat, not hierarchical. Some simple rules to follow when working with multiple threads: Creating threads is expensive, so avoid creating and destroying them rapidly.

What does std :: thread detach do?

std::thread::detachSeparates the thread of execution from the thread object, allowing execution to continue independently. Any allocated resources will be freed once the thread exits. After calling detach *this no longer owns any thread.


2 Answers

How about wrapping it in a pointer?

std::unique_ptr<std::thread> thread_ptr;  // Look into std::make_unique if possible thread_ptr = std::unique_ptr<std::thread>(new std::thread(...)); 

Edit: And yes, the others have mentioned it and I didn't feel the need to add it here, but in order to avoid more downvote piling, I'll say it: You are passing *this and not this thereby copying an instance of your class. (Problems arise because it's non-copyable. Pass this and you should be good to go.)

like image 133
user123 Avatar answered Oct 03 '22 16:10

user123


Your problem is something else - you're passing an instance of MyClass into the thread instead of the pointer to MyClass which the member functions expect. Simply change DoDiskJobThread() like this (do not dereference this):

void MyClass::DoDiskJobThread() {     std::wstring Source = GetSource();     std::wstring Target = GetTarget();     int m_OperationType = GetOperationType();     if      (m_OperationType == OPERATION_COPY)     {         DiskJobThread = std::thread(&MyClass::CopyThread, this, Source, Target);     }     else if (m_OperationType == OPERATION_MOVE)     {         DiskJobThread = std::thread(&MyClass::MoveThread, this, Source, Target);     } } 

You were getting the error because *this resulted in trying to copy MyClass into the thread function, and the copy ctor of your class is deleted (because that of std::thread is deleted). However, the member functions CopyThread and MoveThread require a pointer as the first (hidden) argument anyway.

Live demonstration

like image 27
Angew is no longer proud of SO Avatar answered Oct 03 '22 15:10

Angew is no longer proud of SO