Jump to content
  • Advertisement

Archived

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

kuphryn

function pointer & Thread Parameter :: Multithreading

This topic is 5884 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
Advertisement
quote:
Original post by kuphryn
Is it possible to pass a pointer to a class member function to a worker thread?


Yeah, the create thread functions usually take a void* - you can put a pointer to whatever you want in there.

Share this post


Link to post
Share on other sites
http://www.codeproject.com/threads/thread_win32.asp

nice bit there on threads, and intrestingly how to use a Class to hold the fucntions thus removing the need for global C functions to handle it (tradional way I was using orginaly)

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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!