Mục đích của tôi là giữ đối tượng std::thread
làm thành viên dữ liệu và khởi tạo nó khi cần.
Tôi không thể thực hiện việc này (như trong mã của tôi bên dưới) vì hàm tạo bản sao của lớp std::thread
bị xóa. CÒn cách nào khác để thực hiện việc này không?Có thể định nghĩa std :: thread và khởi tạo nó sau này không?
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);
}
}
Chuyển 'this' thay vì dereferencing' * this'. –