C and C++

Moderators: None (Apply to moderate this forum)
Number of threads: 28629
Number of posts: 94611

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Question on function pointer? Posted by ferdinandng on 8 Jun 2006 at 3:02 AM
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
Report
Re: Question on function pointer? Posted by ferdinandng on 8 Jun 2006 at 3:04 AM
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 settings\ferdinandn\my documents\visual studio 2005\projects\sockettest\mynetwork.cpp 82

which I think is the type difference.

Thank you very much for reading this post.

Best Regards
Ferdinand Ng
Report
Re: Question on function pointer? Posted by Lundin on 8 Jun 2006 at 3:09 AM
: 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 settings\ferdinandn\my documents\visual studio 2005\projects\sockettest\mynetwork.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.
Report
Re: Question on function pointer? Posted by ferdinandng on 8 Jun 2006 at 6:29 AM
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
Report
Re: Question on function pointer? Posted by Lundin on 9 Jun 2006 at 3:26 AM
: 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)?
Report
Re: Question on function pointer? Posted by ferdinandng on 9 Jun 2006 at 4:16 AM

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
Report
Re: Question on function pointer? Posted by Lundin on 9 Jun 2006 at 4:26 AM
Could you post the class implementation as well as other relevant parts of the code?



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.