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) {}
Praetorian
  • 106,671
  • 19
  • 240
  • 328
V Shreyas
  • 449
  • 5
  • 19
  • Weird; I searched for 'std::thread member function' and found the answer: http://stackoverflow.com/questions/10673585/start-thread-with-member-function – kfsone Jun 22 '15 at 06:49
  • I know. I saw that too. – V Shreyas Jun 22 '15 at 06:50
  • So it's the same code, only without the static wrapper. Hint: Remove the static wrapper. – kfsone Jun 22 '15 at 06:50
  • possible duplicate of [Passing member functions to std::thread](http://stackoverflow.com/questions/11057800/passing-member-functions-to-stdthread) – kfsone Jun 22 '15 at 06: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](http://sscce.org) – Praetorian Jun 22 '15 at 06:55
  • Why a -1? I found the question unique. I have searched for days on this. – V Shreyas Jun 22 '15 at 07:03
  • -1 because you chose to add more irrelevant error messages instead of a complete, compilable example. Please also include the compiler version and command line arguments you're invoking the compiler with. Once you do that ping me using @Praetorian and I'll remove the downvote. – Praetorian Jun 22 '15 at 07:09
  • @Praetorian, he provided more error messages because I asked him to. His problem may be different from http://stackoverflow.com/questions/10673585/start-thread-with-member-function , because that solution does not compile for him. – Serge Rogatch Jun 22 '15 at 07:12
  • Not the downvoter, but probably because you don't provide an SSCE/MVCE and there are numerous other questions on the same topic. For some reason, what works for everyone else doesn't work for you, but you don't provide a concrete repro so it's guess work for anyone trying to help. The following compiles just fine (ideone doesn't link it, and functionality is dubious) http://ideone.com/X0mfYD – kfsone Jun 22 '15 at 07:14
  • What is your compiler version? Does it even have full c++11 support? – MikeMB Jun 22 '15 at 07:16
  • typing llvm-g++ --version gave me: Apple LLVM version 6.1.0 (clang-602.0.49) (based on LLVM 3.6.0svn) Target: x86_64-apple-darwin14.3.0 Thread model: posix – V Shreyas Jun 22 '15 at 07:23
  • @kfsone your code, when I ran it on sublime, gave me the same error as my code. That is a good SSCE. – V Shreyas Jun 22 '15 at 07:27
  • @VShreyas, this thing suggests to add -std=c11 to your compiler options: http://stackoverflow.com/questions/29609659/how-can-i-understand-what-standard-my-libraries-are-and-to-what-standard-gcc-com – Serge Rogatch Jun 22 '15 at 07:29
  • @Praetorian I have added an SSCCE now. – V Shreyas Jun 22 '15 at 07:31
  • The error message you posted in an update is a COMPILER error. The error message you linked to is a completely different LINKER error - that doesn't really clarify much and e.g. compiler and linker flags are still missing. But anyway, the linker error from ideone is because `-pthread` is missing from the linker flags. – MikeMB Jun 22 '15 at 07:52
  • @VShreyas Removed my downvote. Your problem most likely is that you need to pass `-std=c++11` to the compiler. You'll probably also need to add `-pthread` or else linking might fail. – Praetorian Jun 22 '15 at 12:34

1 Answers1

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
Serge Rogatch
  • 13,865
  • 7
  • 86
  • 158