Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

std::thread constructor Is there a difference between passing a pointer and passing by ref?

When creating a thread that calls a member function, is there a difference between passing a pointer to the current class or passing a reference?

From the example below, does method1 behave the same as method2? Are there any differences?

class MyClass
{
public:
    MyClass(){};
    ~MyClass(){};
    void memberFunction1()
    {
        //method 1
        std::thread theThread(&MyClass::memberFunction2, this, argumentToMemberFunction2)

        //method 2
        std::thread theThread(&MyClass::memberFunction2, std::ref(*this), argumentToMemberFunction2)
    }
    void memberFunction2(const double& someDouble){};
}
like image 840
user3731622 Avatar asked Dec 17 '15 22:12

user3731622


People also ask

What is the difference between pass by reference and pass-by-pointer?

The difference between pass-by-reference and pass-by-pointer is that pointers can be NULL or reassigned whereas references cannot. Use pass-by-pointer if NULL is a valid parameter value or if you want to reassign the pointer. Otherwise, use constant or non-constant references to pass arguments.

Should I pass by reference or pointer C++?

References are usually preferred over pointers whenever we don't need “reseating”. Overall, Use references when you can, and pointers when you have to. But if we want to write C code that compiles with both C and a C++ compiler, you'll have to restrict yourself to using pointers.

What will happen if passing reference through std :: thread?

If you have used std::thread or std::bind , you probably noticed that even if you pass a reference as parameter, it still creates a copy instead. From cppreference, The arguments to the thread function are moved or copied by value.

Is it better to pass by reference or value?

As a rule passing by const reference is better. But if you need to modify you function argument locally you should better use passing by value. For some basic types the performance in general the same both for passing by value and by reference.


1 Answers

No, there are no differences, but note that using a reference wrapper has only become possible with the acceptance of LWG 2219 as a defect report at the Oct 2015 WG21 meeting.*

Using std::ref may help in cases where you have a named object instance rather than this, since this is quite easy to spell. But consider the following situation, in which you'd like to stay nicely const-correct:

A a;
std::thread(&A::foo, std::cref(a), 1, 2);

This may be easier to read than:

std::thread(&A::foo, &(const_cast<const A&>(a)), 1, 2);
std::thread(&A::foo, &as_const(a), 1, 2);
std::thread(&A::foo, const_cast<const A*>(&a), 1, 2);

*) Vendors that keep distinct language dialects around, like GCC's and Clang with the -std flag), will typically consider defects to apply to all dialects and "fix" the implementations. Defects are things that "were always meant to be the way we say now".

like image 107
Kerrek SB Avatar answered Oct 21 '22 01:10

Kerrek SB