Archived

This topic is now archived and is closed to further replies.

function pointer & Thread Parameter :: Multithreading

This topic is 5575 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi. Is it possible to pass a pointer to a class member function to a worker thread? Otherwise, is it possible to pass in a global function? If yes, do you have to use typedef (*func)(parameters)? Thanks, Kuphryn

Share this post


Link to post
Share on other sites
Thanks.

Magmai Kai Holmlor, I believe you meant passing a function to CreateThread(). I know that is possible. In fact, that is a necessity if you want to call a function to do work.

I am interested more in passing in the thread a function pointer. In other words, I want to call a function while inside the thread.

Kuphryn

Share this post


Link to post
Share on other sites
quote:
Original post by kuphryn
Hi.

Is it possible to pass a pointer to a class member function to a worker thread?

Not directly, assuming a C thread API (i.e. a pointer to a non-member function taking a single void* parameter).

Yes, indirectly; for instance, you put an object pointer and a member function pointer in a suitable struct, and write a global function to unpack those pointers. Pass the global as your thread function, and your struct as your void* parameter, and it''ll do the right thing for you.

quote:
Otherwise, is it possible to pass in a global function?

Yes.

quote:
If yes, do you have to use typedef (*func)(parameters)?

No, but you will have to conform to some prototype or other.

Share this post


Link to post
Share on other sites
Okay. Thanks.

You brought up a solution I missed yesterday. The solution is to encapsulate a function a a struct that is a class member. That will allow you go not have to declare a global function.

The only thing I am uncertain of is if you have a private member struct, will the global worker thread function i.e. the function you pass to __BeginThread() recognize the abstract private member.

Kuphryn

Share this post


Link to post
Share on other sites