My environment is C++ for Linux-Xenomai on ARM gnueabi. After spawning a new pthread successfully I discovered that the class instance was out of scope to the thread. Accessing class instance objects, variables, structures etc. from the thread returned arbitrary values and often 'Segmentation Fault'.
After having spent days of onerous time searching for a solution on the net, I took a guess and tried using the 'this' pointer as argument to pthread_create. And voila! The class instance became visible to the thread. The question is why?
void*(*server_listener_fptr)(void*); // declare the function ptr
server_listener_fptr = reinterpret_cast<void*(*)(void*)>(&UDP_ClientServer::server_listener);
iret = pthread_create(&s_thread, NULL, server_listener_fptr, this);
There is a simple reason why this effectively launches a class instance as an independent thread of a parent process. The debug execution log below sheds some light on the situation. The UDP_ClientServer class instance's ::init() method is entered followed by the creation of a ::server_listener(void*) thread which is a class method of a class instance of the class UDP_ClientServer. The ::init() method that spawned the thread then exits as UDP_ClientServer::init() exit ..., followed shorthy by the class instance method ::server_listener(void*) announcing itself as a thread, as in UDP_ClientServer::server_listener(void*) entry ....
The thread is created as below. The (void*)this pointer is provided as argument passed by of pthread_create to class instance method ::server_listener.
The spawned ::server_listener thread never exits as seen below.
This of course gives the programmer the unique ability to describe complex state machines in a robust concurrent versus sequential processes manner similar to the methods employed in writing RTL in VHDL or Verilog.
The answer to the question is simply that for a class
For the declaration of a class object instance
A call on the class object instance, to a member
Is by definition of the C++ language specification, compiled to
Where the passed reference '&instance' IS the member's 'this' pointer.