invalid conversion from ‘void*’ to ‘void* (*)(void*)’ c++? - c++

I am trying to use pthread_create() but it always gives me this error invalid conversion from void* to void* ( * )(void*)
This error is in the 3rd argument. Could someone help me with this error ?
void Print_data(void *ptr) {
cout<<"Time of Week = " <<std::dec<<iTOW<<" seconds"<<endl;
cout<<"Longitude = "<<lon<<" degree"<<endl;
cout<<"Latitude = "<<lat<<" degree"<<endl;
cout<<"Height Above Sea = "<<alt_MSL<<" meters"<<endl;
}
int call_thread()
{
pthread_create(&thread, NULL, (void *) &Print_data, NULL);
return 0;
}

The error is that you're converting the function pointer (void* (*)(void*)) to an object pointer (void*), when pthread_create expects a function pointer for that argument. There's no implicit conversion to undo the dodgy conversion you've done, hence the error.
The answer is to not do that:
pthread_create(&thread, NULL, &Print_data, NULL);
and you'll also need to modify Print_data to return void* to match the Posix threads interface:
void *Print_data(void *ptr) {
// print stuff
return NULL; // or some other return value if appropriate
}
As noted in the comments, there are various other issues with using this C library directly from C++; in particular, for portability, the thread entry function should be extern "C". Personally, I'd recommend using the standard C++ thread library (or Boost's implementation, if you're stuck with a pre-2011 version of the language).

You're trying to convert a function pointer into a void* here: (void *) &Print_data
According to pthread_create you need to pass in a function that takes a void* and returns a void*
So your function signature should be
void* Print_data(void *ptr)
And your call should be
pthread_create(&thread, NULL, &Print_data, NULL);

pthread_create takes the third argument as
int pthread_create(pthread_t *thread,
const pthread_attr_t *attr,
void *(*start_routine)(void*),
void *arg);
This, void *(*start_routine)(void*) is a pointer to a function that takes a void* pointer and returns a void* pointer.
When you do &Print_data and convert the pointer to void * , it means you are passing a pointer of type void* and not a pointer of type void *(*start_routine)(void*) [function pointer].
To be correct, you need to make your return type as void* and make the call as pthread_create(&thread, NULL, &Print_data, NULL);

You must return void*
void* Print_data(void *ptr) {
to satisfy the needs.
The signature of the function to be passed is
void* function(void*);
then call pthread_create using
pthread_create(&thread, NULL, &Print_data, NULL);

add header file #include
and compile g++ -lpthread

Related

Pthread and function pointers

I am a little confused on how pthread works - specifically, I am pretty sure that pthread takes in a pointer to a function that takes a void pointer as an argument (correct me if I am wrong), and I have declared my function in that way, but I am still getting an error. Here is the code I am struggling with:
void eva::OSDAccessibility::_resumeWrapper(void* x)
{
logdbg("Starting Connection.");
_listener->resume();
logdbg("Connected.");
pthread_exit(NULL);
}
void eva::OSDAccessibility::resumeConnection()
{
long t;
_listener->setDelegate(_TD);
pthread_t threads[1];
pthread_create(&threads[0], NULL, &eva::OSDAccessibility::_resumeWrapper, (void *)t);
}
The error I'm getting is:
No matching function for call to pthread_create.
You don't necessarily have to tell me how to fix the code (although that would be appreciated of course), I'm more interested in why this error is coming up and if my understanding of pthread is correct. Thanks! :)
Your function signature must be void * function (void*)
If called from c++ code, the method must be static:
class myClass
{
public:
static void * function(void *);
}
A solution to use methods that are not static is the following:
class myClass
{
// the interesting function that is not an acceptable parameter of pthread_create
void * function();
public:
// the thread entry point
static void * functionEntryPoint(void *p)
{
((myClass*)p)->function();
}
}
And to launch the thread:
myClass *p = ...;
pthread_create(&tid, NULL, myClass::functionEntryPoint, p);

Encapsulating Threads creates problems

I have the following encapsulation for my pthread_t threads:
#include <pthread.h>
class Thread
{
public:
void run(const int);
static void *run_helper(void *);
bool startThread(int);
void joinThread();
pthread_t th;
};
Where run is my thread routine, and run_helper is the following:
void *Thread::run_helper(int num)
{
return (Thread *)this->run(num);
}
I start my threads like such:
bool Thread::startThread(intptr_t arg)
{
return (pthread_create(&this->th, NULL, &run_helper(arg), (void *)(intptr_t)arg));
}
But when I compile, I get the following errors:
error: lvalue required as unary ‘&’ operand
return (pthread_create(&this->th, NULL, &run_helper(arg), (void *)(intptr_t)arg));
error: ‘this’ is unavailable for static member functions
return (Thread *)this->run(num);
And despite trying, I can't seem to make this encapsulation work.
I think your issue might specifically be &this->th. & has higher precedence than ->. Perhaps try &(this->th).
For the first error, you need to cast the third argument into (void*(*)(void*)) and remove & (maybe the casting isn't neccessary).
pthread_create(&this->th, NULL, (void*(*)(void*))run_helper(arg), (void *)(intptr_t)arg);
The second error, you are trying to use pointer to this in a static function, but the function isn't called on any object, therefore you can't use this in a function like that. The solution is to cast arg into Thread* and then call the non-static run function.

error: argument of type ‘void* (Thread::)(void*)’ does not match ‘void* (*)(void*)’

I'm doing to implement thread class for my own using pthread. So, I create Thread class as below :
class Thread
{
public:
Thread()
{
}
virtual void* run(void *params) = 0;
void start(void *params)
{
pthread_create (&threadId, 0, run, params);
pthread_join (threadId, 0);
}
private:
pthread_t threadId;
};
After implementing this class and override virtual run function, I do compile this project. But error: argument of type ‘void* (Thread::)(void*)’ does not match ‘void* (*)(void*)’ occurs. What is wrong in my code?
Thanks in advance :)
Exactly what the compiler is telling you.
pthread_create is expecting a function with the signature :
void* (*)(void*)
Which is a function pointer.
However, you are providing something with this signature:
void* (Thread::)(void*)
Which is not a function pointer, but a pointer to member function. There is a difference : a pointer to member function needs an instance of an object in order to work properly (here, it would need an instance of Thread).
A usual solution would be to make your function run static : it would not be a member function anymore - it doesn't NEED an instance of Thread in order to work properly anymore, and you could pass your current instance as the last parameter of pthread_create in order to act on it once the thread is launched.
You would just need to save the parameters in the class itself.
public:
void start(void *params)
{
this->my_thread_params = params;
pthread_create (&threadId, 0, run, static_cast<void*>(this));
}
private:
static void *run(void *my_object)
{
// here, my_object already contains the params you passed to the function start
static_cast<Thread*>(my_object)->my_member_function();
}
pthread_create is a C function, and knows nothing of C++ member functions. You'll need to give it a static or non-member function, and pass a pointer to your Thread object via the final argument of pthread_create; something like:
class Thread
{
virtual void* run(void *params) = 0;
void start(void * params)
{
this->params = params;
pthread_create(&threadId, 0, &Thread::static_run, this);
}
static void * static_run(void * void_this)
{
Thread * thread_this = static_cast<Thread*>(void_this);
return thread_this->run(thread_this->params);
}
private:
pthread_t threadId;
void *params;
};
Of course in modern C++, this is rather more straightforward:
std::thread thread;
void start(void * params)
{
thread = std::thread([this]{run(params);});
}
(Although of course you shouldn't be using void* to pass your parameters, and there's probably no good reason to wrap the thread up in a class in the first place.)
The error message is telling you that a pointer to a member function in Thread that takes and returns a void* (void* (Thread::*)(void*)) is not convertible to a pointer to function taking and returning the same void*.
While the declaration of the member function may look similar to the type that you need, there is an implicit this pointer of type Thread that needs to be injected on any call to Thread::run

void* pointer casting while scheduling a thread

I have a function called by a thread.this function has a unique argument which is queue::my_queue . So I need to perform a cast on void pointer in the method called by the thread as follows:
void *AddPacket(void *Ptr)
{ queue<int> my_queue = (queue*)Ptr ;
my_queue.push(byte) ;
}
and in the main, I do:
int main()
{ // do business
pthread_create(&thread, NULL, &AddPacket, (void*)queue) ;
}
But both conversions are wrong.
the first conversion leads to the error:
request for member ‘push' in ‘my_queue’, which is of non-class type ‘queue*’
and the second one:
invalid cast from type ‘queue’ to type ‘void*’
How can I solve the problem?
You try to cast object type to pointer type. It is not allowed. I guess you are new to C++, so I post corrected code here to unpuzzle you and get you going, but please read a book on C++:
queue<int>* my_queue = (queue<int>*)Ptr ;
my_queue->push(byte) ;
pthread_create(&thread, NULL, &AddPacket, &queue) ;
Remember to read about pointers on C++ :)
Try:
queue<int> *my_queue = (queue<int> *)Ptr ;
my_queue->push(byte) ;
pthread_create(&thread, NULL, &AddPacket, (void*)&queue) ;
.. something along those lines, anyway
You need to change both the thread function and the thread creation:
// thread entry point:
void *AddPacket(void *Ptr)
{
reinterpret_cast<std::queue<int>*>(Ptr)->push(byte);
}
// thread creation:
std::queue<int> q;
pthread_create(&thread, NULL, &AddPacket, &q);
// ^^^^ **pointer** to "q";
// conversion to void* is implied

Odd Parameter I dont understand in gridlabd threadpool.c

I'm working on the open project gridlabd hosted at sourceforge. I was trying to call the create_thread function: static __inline int create_thread(void * (*proc)(void *), void *arg)
I just don't understand for the life of me what in the world void * (*proc)(void *) means.
void * (*proc)(void *) is a pointer to a function which returns void* and accepts void* as an argument.
It's a pointer to a function, said function taking a void pointer as an argument and returning a void pointer.
In other words, you can do:
void *threadMain (void *arg) { while (1) doSomething(); }
int stat = create_thread (threadMain, NULL);
to create a thread using the function threadMain as it's procedure.