nerdoza
nerdoza

Reputation: 924

C++ Simple Thread Start Issue

I have fairly simple code, and every example I find looks fairly similar. I'm sure I'm missing something basic, so any help would be greatly appreciated.

/*-------------------------------------
State
--------------------------------------*/
void Receiver::start(int num_threads = 1) {
    _kill_mtx.lock();
    _kill_state = false;
    _kill_mtx.unlock();
    for (int i=0; i<num_threads; ++i)
        threads.push_back(std::thread(thread_func));  // LINE 24

    std::cout << threads.size() << " now running.\n";
    std::cout << std::thread::hardware_concurrency() << " concurrent threads are supported.\n";
}

void Receiver::stop() {

}

/*-------------------------------------
Thread
--------------------------------------*/
void Receiver::thread_func() {
    while(true) {
        if (_kill_mtx.try_lock()) {
            if (_kill_state) {
                break;
            }
            _kill_mtx.unlock();
        }
        std::cout << "Still Alive!" << std::endl;
    }
}

It is compiled using -std=c++0x -lstdc++ -ldbus-c++-1 -pthread and outputs the errors:

 communication.cpp: In member function 'void Receiver::start(int)':
| communication.cpp:24:47: error: no matching function for call to 'std::thread::thread(<unresolved
 overloaded function type>, int&)'
| communication.cpp:24:47: note: candidates are:
| /home/toor/bluegiga-sdk/build/tmp/sysroots/apx4devkit/usr/include/c++/thread:133:7: note: std::th
read::thread(_Callable&&, _Args&& ...) [with _Callable = void (Receiver::*)(int), _Args = {int&}]
| /home/toor/bluegiga-sdk/build/tmp/sysroots/apx4devkit/usr/include/c++/thread:133:7: note:   no kn
own conversion for argument 1 from '<unresolved overloaded function type>' to 'void (Receiver::*&&)
(int)'

The GCC compiler is an ARM compiler provided by the SDK I'm building with. It so far has supported most of the other C++11 features I've tried, so I don't think it's the issue, but I'm not certain.

Upvotes: 0

Views: 105

Answers (1)

juanchopanza
juanchopanza

Reputation: 227578

I am going to guess that Receiver::thread_func is a non-static member function, in which case you need to pass the implicit first parameter. Assuming you want the thread to operate in the same instance, you need to pass this. Otherwise you need to pass a pointer to another Receiver instance:

threads.push_back(std::thread(&Receiver::thread_func, this));

Upvotes: 1

Related Questions