7

I'm testing c++11 threads with this code, but when creating the thread, I'm having the error no matching function for call to 'std::thread::thread()'.

It's like if there was something wrong with the function I'm giving to std::thread ctr, but I don't see how it's wrong. It is incompleted, but it looks right to me:

Header:

#ifndef CONNECTION_H
#define CONNECTION_H

#include <thread>
#include <mysql++.h>

class Connection
{
public:
    Connection(std::string mysqlUser, std::string mysqlPassword);
    ~Connection();

private:
    std::string mysqlUser;
    std::string mysqlPassword;
    std::string mysqlIP;
    int mysqlPort;

    mysqlpp::Connection mysqlConnection;
    std::thread connectionThread;

    void threadLoop();
};

#endif // CONNECTION_H

Source:

#include "connection.h"

Connection::Connection(std::string mysqlUser, std::string mysqlPassword)
{
    this->mysqlUser     = mysqlUser;
    this->mysqlPassword = mysqlPassword;
    this->mysqlIP       = "localhost";    //default
    this->mysqlPort     = 3306;           //default

    //Launch thread
    std::thread connectionThread(threadLoop);

}

Connection::~Connection(){
    mysqlConnection.disconnect();
}

void Connection::threadLoop(){
    //Connect to mySQL database
    mysqlConnection = new mysqlpp::Connection(false);

    if(mysqlConnection.connect(NULL, mysqlIP.c_str(), mysqlUser.c_str(), mysqlPassword.c_str(), mysqlPort)){
        std::string consulta = "SELECT * FROM 'Coordinates'";
        mysqlpp::Query query = mysqlConnection.query(consulta);
        mysqlpp::StoreQueryResult res = query.store();
        query.reset();

    }

    while(true){
        // Stuff
    }
}
12
  • Please provide a complete, minimal sample program. From your description, it should fit in 5-10 lines or so. See SSCCE.ORG.
    – Robᵩ
    Sep 27, 2012 at 14:59
  • 2
    Doesn't std::thread's constructor expect a free function or at the very least a static member function? How can it know on which object to call Connection::threadLoop? Sep 27, 2012 at 15:03
  • Which compiler are you using? Sep 27, 2012 at 15:04
  • @NicolaMusatti: I think you should turn that into an answer.
    – Gorpik
    Sep 27, 2012 at 15:07
  • 1
    You are declaring an new thread object i n your constructor. I doubt this is what you intend. Sep 27, 2012 at 15:26

3 Answers 3

11

The problem is that threadLoop is a member function, but there is no object for it to be applied to. Just guessing:

std::thread connectionThread(&Connection::threadLoop, this);

But that's just the syntactic issue; there's a logic problem, too: that line creates a local object of type std::thread that goes away when the function returns. Its destructor will call std::terminate() because the thread has not been joined. Most likely, this was supposed to attach a thread to the connectionThread member. To do that:

std::thread thr(threadLoop, this);
std::swap(thr, connectionThread);
4
  • In fact, the destructor does not throw, but calls terminate() directly, according to §30.3.1.3 in the standard. Not a good thing in any case. And this will also happen unless connectionThread.join() is eventually called.
    – Gorpik
    Sep 27, 2012 at 16:04
  • It doesn't work, and I don't understand why must I pass a reference to the Connection class with "this": reference says that I should pass a function and optional arguments. Please explain if I am missing something
    – Roman Rdgz
    Sep 28, 2012 at 7:29
  • @RomanRdgz - in what way doesn't it work? A member function can only be called with an object. When you pass a pointer to member function into the thread constructor you must also give it an object. That can be an object itself, a reference to an object, a pointer to an object (such as this), or something that can be dereferenced to get a pointer or reference to an object (typically a smart pointer). Sep 28, 2012 at 14:37
  • Thanks to @Dodgie for the correction of adding &Connection:: to the construction of connectionThread. Don't know why the proposed edit was rejected. May 16, 2013 at 21:49
5

Your code has two problems:

  1. You are providing incomplete information to the std::thread constructor
  2. You are destroying the std::thread before it is joined with the main thread.

For the first problem, as Pete Becker suggests, you need to provide the object on which the function will be called, because the constructor for std::thread has no other way to know it. Assuming that you want to call function threadLoop() on the Connection object you are constructing, you can do this:

//Launch thread
std::thread connectionThread(threadLoop, this);

Internally, the constructor will call this->threadLoop() (where this is the Connection* parameter it received, not the std::thread itself, of course). And you will be fine.

The second problem is that your std::thread is destroyed immediately after starting, without having joined it to the main thread: this will call terminate(), which is not a good thing. Once again, Pete suggests a good alternative. Replace the above code with this:

// Launch thread
std::thread thr(threadLoop, this);
std::swap(thr, connectionThread);

The situation before this code is as follows:

  • You have a trivial std::thread object, connectionThread, which does not really represent a thread

After executing the first line of code:

  • You still have connectionThread
  • You also have a live thread represented by the std::thread object thr, which will be destroyed at the end of the Connection constructor, causing a call to terminate() because it is never joined to the main thread.

Fortunately, the second line of code comes to the rescue. After executing it:

  • You have a trivial std::thread, thr, which can be safely destroyed because it does not represent a real thread (so it is not joinable)
  • You have a live thread represented by connectionThread, an object that will not be destroyed as long as the Connection object exists.

Now, the problem is that you want to join connectionThread to the main thread before it is destroyed, but you also want to avoid blocking the main thread. The right time to do this join is the latest possible time: when connectionThread is about to be destroyed. And this happens at the destructor of Connection. So we'll add a line to this destructor, this way:

Connection::~Connection(){
  mysqlConnection.disconnect();
  connectionThread.join(); // Now connectionThread can be safely destroyed
}

Besides, this is the safest place to call join(), because it ensures that you will never destroy an unjoined connectionThread. This is RAII in action; if you are not familiar with the concept of RAII (or RIIA, as it is sometimes called), you can find a lot of information about this very important concept in the web, including this site.

All this put together: creating a Connection object will create a new thread; in this thread, a new database connection will be established and a query will be executed, while the main thread remains free for whatever other use (for instance, managing the GUI). When the Connection object is finally destroyed, the main thread will wait for the additional thread to finish (if necessary) and then normal execution will continue. I hope this is what you wanted to accomplish with your code.

2

As you may evince from cppreference, std::thread's constructor expect some form of function; you can pass it a free function, a static member function or one of these packed together with its arguments by means of std::bind. In order to execute a non-static member function you should use std::mem_fn to pass it together with the object it should be called on.

6
  • 3
    There's no need for std::bind or std::men_fn here; std::thread's constructor knows how to deal with member functions. The problem is that there is no object for the member function to be applied to. Sep 27, 2012 at 15:36
  • I see. I don't have the standard at hand and cppreference is not very clear on this subject. Sep 27, 2012 at 15:37
  • 1
    cppreference is quite clear, but it's wrong. <g> For TR1, we invented the INVOKE terminology to describe how bind and function deal with various callable types, including pointers to member functions (the second argument has to be an object, a reference to an object, or a pointer to an object of a suitable type). std::thread::thread(Fn&&, Args&&...) uses the same mechanism. Sep 27, 2012 at 15:46
  • @PeteBecker: I am reading the standard now and see that you are right. And now I read your comment and see that you had already explained the mechanism :)
    – Gorpik
    Sep 27, 2012 at 15:54
  • I don't understand why giving std::thread() a free function works, and using a function declared into a class doesn't. Anyway, If I did my function free, out from the class, it still doesn't work
    – Roman Rdgz
    Sep 28, 2012 at 7:28

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.