mardi 23 juin 2015

Why does std::thread take function to run by rvalue?

There is one thing about std::thread which I don't understand: why the constructor of std::thread takes function to run by rvalue?

I usually want to run a Functor with some members to another thread. Like this:

struct Function
{
    void operator() ( /* some args */)
    {
        /* some code */
    }

    /* some members */
}


void run_thread()
{
    Functor f( /* some data */);
    std::thread thread(f, /* some data */);

    /* do something and wait for thread to finish */
}

With current implementation of std::thread I must be sure my object is implementing move semantics. I don't get why cannot I pass it by reference.

Extra question is: what does it mean to refer to function by rvalue? Lambda expression?

Aucun commentaire:

Enregistrer un commentaire