Build failure when creating a Thread

I am trying to create a Thread, and have the code:

 Thread("TcpClientManagerThread", _tcpClientThread, OS_THREAD_PRIORITY_DEFAULT);

where the thread function is declared as:

void _tcpClientThread() {
    for(;;) {        
        // do something here
    }
}

However, when I build it I get the error:

../wiring/inc/spark_wiring_thread.h:53:5: note: Thread::Thread(const char*, wiring_thread_fn_t, os_thread_prio_t, siz
e_t)
     Thread(const char *name, wiring_thread_fn_t function,
     ^
../wiring/inc/spark_wiring_thread.h:53:5: note:   no known conversion for argument 2 from '<unresolved overloaded fun
ction type>' to 'wiring_thread_fn_t {aka std::function<void()>}'

Am I declaring the Thread/function correctly?

We’d need to see the whole error output.
You’ve only posted the notes but I can’t see an error message.

OK, here is the whole response (using particle-cli for the build):

    TcpClientComms.cpp: In member function 'void TcpClientComms::Initialise()':
TcpClientComms.cpp:7:98: error: no matching function for call to 'Thread::Thread(const char [23], <unresolved overloaded function type>, const os_thread_prio_t&)'
     Thread("TcpClientManagerThread", TcpClientComms::_tcpClientThread, OS_THREAD_PRIORITY_DEFAULT);
                                                                                                  ^
TcpClientComms.cpp:7:98: note: candidates are:
In file included from ../wiring/inc/spark_wiring_watchdog.h:22:0,
                 from ../wiring/inc/spark_wiring_cloud.h:31,
                 from ../wiring/inc/spark_wiring.h:48,
                 from ./inc/application.h:36,
                 from ./inc/Particle.h:5,
                 from TcpClientComms.h:4,
                 from TcpClientComms.cpp:1:
../wiring/inc/spark_wiring_thread.h:53:5: note: Thread::Thread(const char*, wiring_thread_fn_t, os_thread_prio_t, size_t)
     Thread(const char *name, wiring_thread_fn_t function,
     ^
../wiring/inc/spark_wiring_thread.h:53:5: note:   no known conversion for argument 2 from '<unresolved overloaded function type>' to 'wiring_thread_fn_t {aka std::function<void()>}'
../wiring/inc/spark_wiring_thread.h:46:5: note: Thread::Thread(const char*, os_thread_fn_t, void*, os_thread_prio_t, size_t)
     Thread(const char* name, os_thread_fn_t function, void* function_param=NULL,
     ^
../wiring/inc/spark_wiring_thread.h:46:5: note:   no known conversion for argument 2 from '<unresolved overloaded function type>' to 'os_thread_fn_t {aka void (*)(void*)}'
../wiring/inc/spark_wiring_thread.h:44:5: note: Thread::Thread()
     Thread() : handle(OS_THREAD_INVALID_HANDLE) {}
     ^
../wiring/inc/spark_wiring_thread.h:44:5: note:   candidate expects 0 arguments, 3 provided
../wiring/inc/spark_wiring_thread.h:37:7: note: constexpr Thread::Thread(const Thread&)
 class Thread
       ^
../wiring/inc/spark_wiring_thread.h:37:7: note:   candidate expects 1 argument, 3 provided
make[1]: *** [../build/target/user/platform-6TcpClientComms.o] Error 1
make: *** [user] Error 2

You can’t use a non-static function (method) of an object as parameter for Thread::Thread() since the call is unresolvable at compile time.

You’d either need to make the function static or need an overload for Thread::Thread() that also takes an instance pointer to your object (which is not implemented AFAIK).

2 Likes

Thanks, making the method static worked fine (well it builds anyway). Out of curiosity how did you manage to diagnose the problem from the log? I cannot see anything in there that would point me in that direction.

This is the actual error message

TcpClientComms.cpp:7:98: error: no matching function for call to 'Thread::Thread(const char [23], <unresolved overloaded function type>, const os_thread_prio_t&)'
     Thread("TcpClientManagerThread", TcpClientComms::_tcpClientThread, OS_THREAD_PRIORITY_DEFAULT);

From this part TcpClientComms::_tcpClientThread and the statement <unresolved overloaded function type> I gathered that you intended to provide a non-static function as thread function and knowing how C++ does do the target resolution of functions like this made it clear what the issue was.

Hence the absolute need to provide all the error output you get if someone else should make some sense of it without seeing the (complete) code.
But also providing the code is always advisable as well.

e.g. you code snippet in your first post didn’t really tell the whole story :wink:
There it wasn’t clear that you were dealing with a class nor that the thread function would be a non-static member of that class.

1 Like

Makes sense. Thanks.