Guys,
I have a problem which takes me 2 days to solve so far, hope you may help me.
I have posted a thread before about it but unfortunately the problem is still there!! http://www.thescripts.com/forum/thread647482.html
-----
the problem is simply "I do not know what is the correct syntax if I want to call a function in POSIX thread create which has an argument which is pointer to function". -
-
class EventInterface {
-
public:
-
EventInterface(void (*ptToFunc)(Event *, int));
-
.....
-
static void * receiveEvent (void (*onMessageFunc)(Event *, int));
-
-
}
-
-
EventInterface::EventInterface(void (*ptToFunc)(char *, int)){
-
-
pthread_create( &receiveThread, NULL, EventInterface::receiveEvent, (void *) (*ptToFunc)) ?????
-
....
-
-
}
-
-
void EventInterface::receiveEvent(void (*onMessageFunc)(Event *, int)){
-
.....
-
-
onMessageFunc(notifiedEvent , subscriptionID);
-
}
-
-
I really have no idea, I almost check all of possible syntaxes but they do not work...
The errors I received are basically because of the syntax faults. (mostly concerning the "invalid conversion from ....")
Thanks,
Amir.
9 3353
Your problem is the function pointer.
For Windows, your thread function you supply to CreateThread must be: -
DWORD WINAPI THreadProc(LPVOID);
-
You can write any function BUT the function must have an LPVOID argument and return a DWORD. If it doesn't, then it can't be used as a thread.
Read the pthread_create() documentation clearly and focus on the type of function pointer required. You may need to type cast your function to the correct type.
Your problem is the function pointer.
For Windows, your thread function you supply to CreateThread must be:
[code=cpp]
DWORD WINAPI THreadProc(LPVOID);
[/cpp]
You can write any function BUT the function must have an LPVOID argument and return a DWORD. If it doesn't, then it can't be used as a thread.
Read the pthread_create() documentation clearly and focus on the type of function pointer required. You may need to type cast your function to the correct type.
I am working with linux.
I just have one question: what is the correct syntax to call a method with function pointer as an argument in pthered_create?
forexample when I define this like:
pthread_create( &receiveThread, NULL, EventInterface::receiveEvent, (void *) (*ptToFunc))
or
pthread_create( &receiveThread, NULL, EventInterface::receiveEvent, (void *) ptToFunc)
I have this error:
error: invalid conversion from 'void* (*)(void (*)(Event*, int))' to 'void* (*)(void*)'
pthread_create has this prototype: -
int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
-
void *(*start_routine)(void*), void *arg);
-
-
The start routine is:
void *(*start_routine)(void*)
That is, start_routine is a pointer to a function with a void* argunment that returns a void*.
Your function must have thisprototype.
However, this function: -
void (*ptToFunc)(Event *, int));
-
does not have a void* argument and does not return a void*. So you can't use this function as a thread.
Yout could use:
This is exactly the procedure I outlined in the Windows eaxmple. The OS function cannot accommodate just any old function. It has to be of a pre-defined format.
Inside MyFunction(void* arg) you know the arg is an Event* so it is safe to type cast the void* to an Event* and off you go.
I guess I find a way to solve the problem. -
-
class EventInterface {
-
public:
-
typedef void (*onMessageFunction)(Event*, int);
-
-
-
EventInterface(onMessageFunction);
-
-
}
-
-
EventInterface::EventInterface(onMessageFunction pt2Func){
-
-
pthread_create( &receiveThread, NULL, EventInterface::receiveEvent, (void *) pt2Func) *
-
-
}
-
-
void EventInterface::receiveEvent(void * arg){
-
-
onMessageFunction onMessageFunc; *
-
onMessageFunc= (onMessageFunction) arg;
-
-
onMessageFunc(notifiedEvent , subscriptionID);
-
}
-
-
However now I see some warning like:
ISO C++ forbids casting between pointer-to-function and pointer-to-object
for those lines I showed by *.
Do you have any idea?
Did you read Post #5??
actually I read that after I post my new post!
The problem is the name of call back function is called from EventInterface constructor. so how could it be possible with your solution?!
maybe I need to read your solution more accurately.
for those who might see this thread then... I finally come up with a syntax which WORKS!
I just wrap my pointer to function in an struct!\ -
-
class EventInterface
-
{
-
-
public:
-
-
typedef struct{
-
void (*onMessageFunction)(Event*, int);
-
} callBackFunc;
-
-
EventInterface(callBackFunc);
-
...
-
static void * receiveEvent (void *);
-
-
}
-
-
EventInterface::EventInterface(callBackFunc ptToFunc){
-
-
pthread_create( &receiveThread, NULL, EventInterface::receiveEvent, (void *) &ptToFunc)
-
-
}
-
-
void * EventInterface::receiveEvent(void * arg){
-
-
callBackFunc * onMessageFunc;
-
onMessageFunc= (callBackFunc*) arg;
-
-
.....
-
-
onMessageFunc->onMessageFunction(notifiedEvent , subscriptionID);
-
}
-
-
for those who might see this thread then... I finally come up with a syntax which WORKS!
I just wrap my pointer to function in an struct!\ -
-
class EventInterface
-
{
-
-
public:
-
-
typedef struct{
-
void (*onMessageFunction)(Event*, int);
-
} callBackFunc;
-
-
EventInterface(callBackFunc);
-
...
-
static void * receiveEvent (void *);
-
-
}
-
-
EventInterface::EventInterface(callBackFunc ptToFunc){
-
-
pthread_create( &receiveThread, NULL, EventInterface::receiveEvent, (void *) &ptToFunc)
-
-
}
-
-
void * EventInterface::receiveEvent(void * arg){
-
-
callBackFunc * onMessageFunc;
-
onMessageFunc= (callBackFunc*) arg;
-
-
.....
-
-
onMessageFunc->onMessageFunction(notifiedEvent , subscriptionID);
-
}
-
-
Don't do it. It will mess up. Get rid of the address operator. You are passing the address of where the callback pointer is stored on the stack. - pthread_create( &receiveThread, NULL, EventInterface::receiveEvent, (void *) ptToFunc)
.
Adrian
Sign in to post your reply or Sign up for a free account.
Similar topics
by: raxitsheth |
last post by:
Hello All...
I am using Posix Thread.
class Parent
{
public: virtual void* func(void *)=0;
|
by: rnn98 |
last post by:
hi,
my multithread application, running under solaris box, is crashing
eventually.
I tried to spot and substitute functions not "thread safe", but I
guess my search wasn't good enough. I have put...
|
by: sathya_me |
last post by:
friends,
As I was going through a set of past thread I have some doubts in that.
The link to the thread is:
...
|
by: Jonathan Burd |
last post by:
Greetings everyone,
Here is a random string generator I wrote for an application
and I'm wondering about the thread-safety of this function.
I was told using static and global variables cause...
|
by: Simon Verona |
last post by:
I would normally use code such as :
Dim Customer as new Customer
Dim t as new threading.thread(AddressOf Customer.DisplayCustomer)
Customer.CustomerId=MyCustomerId
t.start
Which would create...
|
by: Abubakar |
last post by:
Hi,
Lets say I have a method called "listen_proc" inside "class1". There is
another method called "start" in the same class that has to start the
"listen_proc" inside a new thread. I am using...
|
by: is_vlb50 |
last post by:
I have problem with pass structure to thread function in SOLARIS.When I
pass it to created thread it always has empty values.
my structure defined as:
typedef struct momMSG{
struct mbhdr hd;...
|
by: NaeiKinDus |
last post by:
Hello, i'm trying to program a thread that would be locked (by a
mutex) and that would only be unlocked once that a function
(generating data) is done. The purpose is to generate data, and unlock...
|
by: Klaus |
last post by:
Hi,
I have an existing VC 6 MFC application which communicates asynchronly with
a VC 2005 managed code dll.
I use an unmanaged base class with virtual functions to access methods in
the MFC...
|
by: andreas.zetterstrom |
last post by:
I'm implementing some different c++ classes which I want to be thread
safe. All these classes contain lists or arrays of some kind.
I'm using protected sections to make them thread safe.
The...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |