Sign in to follow this  
_Madman_

Dam compiler is teaching me what to do!!!

Recommended Posts

_Madman_    100
I have a function -> unsigned long __stdcall CClass::threadfunc(void *param); and I want to make a thread with it -> _beginthreadex(NULL, 0, (unsigned long (__stdcall *)(void *))CClass::threadfunc, NULL, 0, &thread_1); Compiler is sending me for a walk no matter what I do. Dam I hate when you have to dance around some supid checks just to feed 32 bit function adress to a stupid machine!!! Does anyone know what I'm doing wrong?!

Share this post


Link to post
Share on other sites
CTar    1134
There's a big difference between static and non-static functions. Therefore if threadfunc isn't static then it isn't just a 32 bit function address, you need to pass the object to operate on somewhere to.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
Member functions are totally different than static or global functions (they have a different calling convention, because they need access to a this pointer). If you want to use a member function like that, you'll need some global wrapper to pass to beginthread()

Share this post


Link to post
Share on other sites
Evil Steve    2017
Use a static function to pass it off to the non-static one:

class CClass
{
public:
// Normal methods

private:
static unsigned long __stdcall staticthreadfunc(void *param)
{ return ((CClass*)param)->threadfunc(); }
unsigned long threadfunc(); // Implement as normal
};

// Call it like this:
_beginthreadex(NULL, 0, staticthreadfunc, this, 0, &thread_1);


Share this post


Link to post
Share on other sites
rohde    432
If you think about it it's pretty logical that you can't pass a pointer to a member function since a member-function is useless outside the context of an object.

Use a global a global function as a wrapper instead.

(oh man gamedev is fried right now....getting so many errors)

Share this post


Link to post
Share on other sites
Verg    450
Evil Steve's implementation allows you to wrap threads in objects. We've called the initial __stdcall thread a "thread bounce" function around here.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this