Reputation: 321
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?
Upvotes: 2
Views: 2148
Reputation: 2594
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++ :)
Upvotes: 4
Reputation: 477562
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
Upvotes: 1
Reputation: 24907
Try:
queue<int> *my_queue = (queue<int> *)Ptr ;
my_queue->push(byte) ;
pthread_create(&thread, NULL, &AddPacket, (void*)&queue) ;
.. something along those lines, anyway
Upvotes: 3