Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compiler error using std::shared_ptr with std::thread

Tags:

c++

c++11

I'm trying to start thread using a shared_ptr from class Test, and I get this error:

/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/functional:559:2: note: no known conversion for argument 1 from 'std::shared_ptr<Test>' to 'std::shared_ptr<Test>&'

Example Code:

    std::shared_ptr<Test> test = std::make_shared<Test>();
    std::thread th(&Test::run, test); // Compiler error


    Test* test2 = new Test;
    std::thread th(&Test::run, test2); // okay

Note: In windows with VS2013 works fine the first example.

like image 325
davidaristi Avatar asked May 30 '14 20:05

davidaristi


1 Answers

This looks like a bug in the gcc version you're using, as it should work. And looking at http://ideone.com/GOQ35M it does work

As a workaround, you can try

std::shared_ptr<Test> test = std::make_shared<Test>();
std::thread th(std::bind(&Test::run, test))
like image 165
Dave S Avatar answered Nov 05 '22 03:11

Dave S