Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Question on function pointer?

ferdinandngferdinandng Posts: 62Member
Hi Experts,

I am implementing a thread which consist will call the pass in function (using function pointer) continueously.

My thread class is the following:

lass MyThread : public Thread
{

private:
int m_nCount;
bool status;

// function pointer
void (*ptr) (void);

HANDLE eventRun; // event for Run
HANDLE eventDie; // event for Die
HANDLE lpHandles[2];
void * baseClass; // point to the base class

public:
MyThread(int n, const char* nm, void * base);
~MyThread();

void myThreadFunction(void (*funcPtr) (void)); // (the problem method A)
void triggerEvent(EVENT e);
void initEvent();
void run();

};

When I create the thread object, I will pass in a function pointer like this:

>> myThread = new MyThread(1, "MyListener", this);
>> void (CMyNetwork::*ptr) (void);
>> ptr = &CMyNetwork::listenerMethod;
>> myThread->myThreadFunction(ptr);
>> myThread->start();

where listenerMethod is defined like this:

>> void listenerMethod(void);

I realize that the problem error is wrong, since my function pointer is pointing to an member function of the class CMyNetwork::listenerMethod, but I want the thread object independent to the network (cause of the two class linking to each other). Is there solution for my problem.

Or if there is an alternative way to implement that.

Thank you very much for reading this post.

Best regards
Ferdinand Ng

Comments

  • ferdinandngferdinandng Posts: 62Member
    Hi Experts,

    I forgot to mention the error for my implementation is:

    Error 1 error C2664: 'MyThread::myThreadFunction' : cannot convert parameter 1 from 'void (__thiscall CMyNetwork::* )(void)' to 'void (__cdecl *)(void)' c:documents and settingsferdinandnmy documentsvisual studio 2005projectssockettestmynetwork.cpp 82

    which I think is the type difference.

    Thank you very much for reading this post.

    Best Regards
    Ferdinand Ng
  • LundinLundin Posts: 3,711Member
    : Hi Experts,
    :
    : I forgot to mention the error for my implementation is:
    :
    : Error 1 error C2664: 'MyThread::myThreadFunction' : cannot convert parameter 1 from 'void (__thiscall CMyNetwork::* )(void)' to 'void (__cdecl *)(void)' c:documents and settingsferdinandnmy documentsvisual studio 2005projectssockettestmynetwork.cpp 82
    :
    : which I think is the type difference.
    :
    : Thank you very much for reading this post.
    :
    : Best Regards
    : Ferdinand Ng
    :


    I think it is because the thread function needs to be static, assuming that your threads are based on the Win32 threads.
  • ferdinandngferdinandng Posts: 62Member
    Hi Lundin,

    Thanks for your reply. I made the thread static and it doesn't solve the problem. I found it so difficult to use the function pointer >.<. Do you have any other method which can pass in a function as a parameters, so that the thread can call the corresponding method.

    Thank you very much for reading this post.

    Best Regards
    Ferdinand Ng
  • LundinLundin Posts: 3,711Member
    : Hi Lundin,
    :
    : Thanks for your reply. I made the thread static and it doesn't solve the problem. I found it so difficult to use the function pointer >.<. Do you have any other method which can pass in a function as a parameters, so that the thread can call the corresponding method.
    :
    : Thank you very much for reading this post.
    :
    : Best Regards
    : Ferdinand Ng
    :


    Perhaps I misunderstood what you are trying to do... will the function pointer passed as parameter be a pointer to the thread function itself or a pointer to a function called from within the thread function? Or something else?

    And what Thread class are you using? Is it a compiler specific class or is it something you have written yourself? If written by yourself, which OS and thread API is used (CreateThread, beginthread, pthread)?
  • ferdinandngferdinandng Posts: 62Member

    Hi Lundin,

    Sorry for the misunderstanding, I am trying to pass a function into a thread class, which allows the thread class to call on the pass in function.

    I am using Visual C++ 2005 in Windows XP and implement a simple thread class myself using createThread method. Actually what I want to do is similar to like the createThread method, which takes in a function.

    Thank you very much for you help and thank you for your valuable time.

    Best Regards
    Ferdinand Ng
  • LundinLundin Posts: 3,711Member
    Could you post the class implementation as well as other relevant parts of the code?
Sign In or Register to comment.