1

I have a class containing a member function which I want to pass to std::thread's constructor.

#include <thread>
#include <iostream>

struct StatsClientImpl
{
    std::thread t;
    size_t q_len;

    StatsClientImpl() : q_len(0)
    {
        t = std::thread(&StatsClientImpl::de_q, this);
    }

    ~StatsClientImpl()
    {
        if (t.joinable())
            t.join();
    }

    void de_q()
    {
        std::cout << "in de_q\n";
    }
};

int main()
{
    StatsClientImpl s;
}

I get the following errors:

/Users/apple/platform/stats-client/src/main/cpp/StatsClientImpl.cpp:21:17: error: no matching constructor for initialization of 'std::thread'
    std::thread te(&StatsClientImpl::de_q, this);
                ^  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/thread:374:9: note: candidate constructor template not viable: requires single argument '__f', but 2 arguments were provided
thread::thread(_Fp __f)
        ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/thread:263:5: note: candidate constructor not viable: requires 1 argument, but 2 were provided
    thread(const thread&);
    ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/thread:270:5: note: candidate constructor not viable: requires 0 arguments, but 2 were provided
    thread() _NOEXCEPT : __t_(0) {}
16
  • Weird; I searched for 'std::thread member function' and found the answer: stackoverflow.com/questions/10673585/… Commented Jun 22, 2015 at 6:49
  • I know. I saw that too. Commented Jun 22, 2015 at 6:50
  • So it's the same code, only without the static wrapper. Hint: Remove the static wrapper. Commented Jun 22, 2015 at 6:50
  • possible duplicate of Passing member functions to std::thread Commented Jun 22, 2015 at 6:51
  • I don't see anything wrong with your code. As others have mentioned, passing the this pointer through the static function is not required. If you're still getting errors, please post an SSCCE Commented Jun 22, 2015 at 6:55

1 Answer 1

1

C++11 thread allows to call a non-static method directly. The syntax you used is intended for that, just replace de_q_caller with de_q:

t = std::thread(&StatsClientImpl::de_q, this);

UPDATE: because your compiler does not seem to allow that, try

t = std::thread(std::bind(&StatsClientImpl::de_q, this));

clang compiler may require to add the following compiler option:

-std=c++11
Sign up to request clarification or add additional context in comments.

6 Comments

That is strange. Can you post the definition of method de_q ?
Does it compile if using std::bind? t = std::thread(std::bind(&StatsClientImpl::de_q, this));
Didn't try that. Can you tell me how?
it is giving me an error: invalid argument '-std=c11' not allowed with 'C++/ObjC++'
Oops, the option should be -std=c++11 . Have you tried std::bind?
|

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.