7

When you construct a new thread the supplied function object is copied into the storage belonging to the newly created thread. I want to execute an object method in a new thread. The object should not be copied. So I pass shared_ptr of the object to std::thread constructor. How can I launch a new thread with std::shared_ptr() object? For example

class Foo {
public: 
    void operator()() {       
        // do something
    }    
};

int main() {
    std::shared_ptr<Foo> foo_ptr(new Foo);

    // I want to launch a foo_ptr() in a new thread
    // Is this the correct way?
    std::thread myThread(&Foo::operator(), foo_ptr.get());  
    myThread.join();     
}
11
  • 2
    Include all error messages and observations in your question please, that's what the linked policy says. VTC your question because of lacking this now, good evening. Commented Sep 18, 2015 at 16:19
  • 2
    you are nitpicking on missing includes and small stuff. Just let it go.. Commented Sep 18, 2015 at 16:55
  • 2
    I'm your soup nazi here of course ;-). Please improve your question, to be complete and helpful for future research. Commented Sep 18, 2015 at 17:06
  • 2
    :) I'm no longer engaging in this. I believe the question is clear as is. If you feel like the question should be closed it's fine. If you want to improve it by editing, it's fine too. Commented Sep 18, 2015 at 17:18
  • 7
    Look I've edited a number of other user's questions to improve their quality. Even though my code was a sketch, it's very minimal and clear. This site has helpful community in general. I don't support the way you handle this Commented Sep 18, 2015 at 17:30

2 Answers 2

11

You overcomplicate the issue, just pass std::shared_ptr itself, std::bind and std::thread know how to deal with it:

std::thread myThread( &Foo::operator(), foo_ptr );  

This way std::thread instance will share ownership and that would guarantee object would not be destroyed before myThread

Sign up to request clarification or add additional context in comments.

2 Comments

Would that keep a reference to the shared_ptr when detach() is called on thread and the thread object is destroyed?
I tested it in MSVC: seems it does keep the shared_ptr refcount alive after detach. ideone.com/S9ZLyG
5

You have a lot of errors in you sketch code. I think a more readable way would be to use a lambda that captures the sptr by value and then deferences it to call the function.

#include <thread>
#include <memory>
#include <cstdio>

class Foo
{
public:
    void operator()()
    {
        printf("do something\n");
    }
};

int main()
{
    auto foo_ptr = std::make_shared<Foo>();

    std::thread myThread([foo_ptr] { (*foo_ptr)(); });
    myThread.join();
}

It is not perfectly clear what you want to do. If you want to launch a new thread that runs a member function without copying the class instance the syntax would be one of those:

Foo foo;
std::thread th1(&Foo::moo, &foo);
std::thread th2([&]{ foo.moo(); });

2 Comments

Isn't a std::make_shared missing also?
@πάνταῥεῖ Yes. The code actually worked when testing, because the member function does not use the this pointer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.