C ++ copy constructor in respect of thread

An example of this comes from the cause of "C ++ concurrent programming actual combat," the

#include <thread>
#include <iostream>
#include <stdexcept>

class ScropeThread
{
public:
    ScropeThread(std::thread t) :m_pThead(std::move(t))
    {
        if (!m_pThead.joinable())
        {
            throw std::logic_error("no thread");
        }
    }
    ~ScropeThread()
    {
        m_pThead.join();
    }
    ScropeThread(const ScropeThread &) = delete;
    ScropeThread& operator=(const ScropeThread &) = delete;

private:
    std::thread m_pThead;
};

void fun(void){}
int main()
{
    ScropeThread(std::thread(fun));

    return 0;
}

 

I "had an idea", will become the main function of this sub-sub:

int main()
{
    std::thread t1(fun);
    ScropeThread st(t1);

    return 0;
}

The compiler did not hesitate to give me the error:

One is to define a thread object t1, t1 is then passed as a constructor argument scropethread of a definition of a temporary thread object, and then pass the parameter list scropethread.

Both methods require the use of the copy constructor arguments copied into the parameter , i.e., that both methods use a copy constructor thread class .

The reason given is that employing words thread class does not define a copy constructor. I could not understand.

Toss for nearly two hours. Suddenly blessing to the soul, c ++ think in the right value concept. Right yo, the first way when the argument to the constructor scropethread, I created a list of parameters in the temporary  std :: thread (fun), this is an rvalue .

And when I'm out of this definition: std :: thread t1 (fun) ; t1 is a left value .

The real cause of the error is: Thread class is not defined as a value to the left of the copy constructor parameters, but to have the right value of the parameter as a copy constructor.

Point into the header file thread class is really the case:

File from scratch, we can draw a conclusion: the Thread objects can not be copied (copy), and can only move (the Move) .

Guess you like

Origin www.cnblogs.com/XiaoXiaoShuai-/p/11334374.html