473,398 Members | 2,404 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

C++ - Thread - Pointer to function!

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".

Expand|Select|Wrap|Line Numbers
  1.  
  2. class EventInterface {
  3.    public:
  4.      EventInterface(void (*ptToFunc)(Event *, int));
  5.      .....
  6.      static void * receiveEvent (void (*onMessageFunc)(Event *, int));
  7.  
  8. }
  9.  
  10.    EventInterface::EventInterface(void (*ptToFunc)(char *, int)){
  11.  
  12.             pthread_create( &receiveThread, NULL, EventInterface::receiveEvent, (void *) (*ptToFunc))    ?????
  13.           ....
  14.  
  15.       }
  16.  
  17.    void EventInterface::receiveEvent(void (*onMessageFunc)(Event *, int)){
  18.    .....
  19.  
  20.     onMessageFunc(notifiedEvent , subscriptionID);
  21.     }
  22.  
  23.  
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.
May 22 '07 #1
9 3353
weaknessforcats
9,208 Expert Mod 8TB
Your problem is the function pointer.

For Windows, your thread function you supply to CreateThread must be:
Expand|Select|Wrap|Line Numbers
  1. DWORD WINAPI THreadProc(LPVOID);
  2.  
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.
May 22 '07 #2
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?
May 22 '07 #3
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*)'
May 22 '07 #4
weaknessforcats
9,208 Expert Mod 8TB
pthread_create has this prototype:

Expand|Select|Wrap|Line Numbers
  1. int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
  2.     void *(*start_routine)(void*), void *arg);
  3.  
  4.  
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:
Expand|Select|Wrap|Line Numbers
  1. void (*ptToFunc)(Event *, int));
  2.  
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:
Expand|Select|Wrap|Line Numbers
  1. void *MyFunction(void*);
  2.  
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.
May 22 '07 #5
I guess I find a way to solve the problem.


Expand|Select|Wrap|Line Numbers
  1.  
  2.  class EventInterface {
  3.             public:
  4.         typedef void (*onMessageFunction)(Event*, int);
  5.  
  6.  
  7.         EventInterface(onMessageFunction);
  8.  
  9.     }
  10.  
  11.         EventInterface::EventInterface(onMessageFunction pt2Func){
  12.  
  13.                   pthread_create( &receiveThread, NULL, EventInterface::receiveEvent, (void *) pt2Func)                   *
  14.  
  15.         }
  16.  
  17.        void EventInterface::receiveEvent(void * arg){
  18.  
  19.       onMessageFunction onMessageFunc;                               *
  20.           onMessageFunc= (onMessageFunction) arg;        
  21.  
  22.           onMessageFunc(notifiedEvent , subscriptionID);
  23.        }
  24.  
  25.  
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?
May 22 '07 #6
weaknessforcats
9,208 Expert Mod 8TB
Did you read Post #5??
May 22 '07 #7
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.
May 22 '07 #8
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!\

Expand|Select|Wrap|Line Numbers
  1.  
  2. class EventInterface 
  3. {
  4.  
  5. public:
  6.  
  7.     typedef struct{
  8.         void (*onMessageFunction)(Event*, int);
  9.     } callBackFunc;
  10.  
  11.     EventInterface(callBackFunc);
  12.         ...
  13.     static void * receiveEvent (void *);
  14.  
  15. }
  16.  
  17. EventInterface::EventInterface(callBackFunc ptToFunc){
  18.  
  19.     pthread_create( &receiveThread, NULL, EventInterface::receiveEvent, (void *) &ptToFunc)
  20.  
  21. }
  22.  
  23. void * EventInterface::receiveEvent(void * arg){
  24.  
  25.     callBackFunc * onMessageFunc;
  26.     onMessageFunc= (callBackFunc*) arg;
  27.  
  28.        .....
  29.  
  30.        onMessageFunc->onMessageFunction(notifiedEvent , subscriptionID);
  31. }
  32.  
  33.  
May 23 '07 #9
AdrianH
1,251 Expert 1GB
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!\

Expand|Select|Wrap|Line Numbers
  1.  
  2. class EventInterface 
  3. {
  4.  
  5. public:
  6.  
  7.     typedef struct{
  8.         void (*onMessageFunction)(Event*, int);
  9.     } callBackFunc;
  10.  
  11.     EventInterface(callBackFunc);
  12.         ...
  13.     static void * receiveEvent (void *);
  14.  
  15. }
  16.  
  17. EventInterface::EventInterface(callBackFunc ptToFunc){
  18.  
  19.     pthread_create( &receiveThread, NULL, EventInterface::receiveEvent, (void *) &ptToFunc)
  20.  
  21. }
  22.  
  23. void * EventInterface::receiveEvent(void * arg){
  24.  
  25.     callBackFunc * onMessageFunc;
  26.     onMessageFunc= (callBackFunc*) arg;
  27.  
  28.        .....
  29.  
  30.        onMessageFunc->onMessageFunction(notifiedEvent , subscriptionID);
  31. }
  32.  
  33.  
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.
Expand|Select|Wrap|Line Numbers
  1.     pthread_create( &receiveThread, NULL, EventInterface::receiveEvent, (void *) ptToFunc)
.


Adrian
May 23 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: raxitsheth | last post by:
Hello All... I am using Posix Thread. class Parent { public: virtual void* func(void *)=0;
9
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...
5
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: ...
4
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...
6
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...
2
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...
3
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;...
3
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...
3
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...
3
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...
0
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
marktang
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,...
0
jinu1996
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...
0
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...
0
tracyyun
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...
0
agi2029
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,...
0
isladogs
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.