(__cdecl *)?

I’m affraid all of this is just a matter of C/C++ conversion stuff, i hope
i’m wrong.
I got to the point where my SDL app has to use multithreading. So I used the
function :
SDL_Thread *SDL_CreateThread(int (*fn)(void *), void *data);
to create a thread in a C program.

Now i’m coding in C++ and i’d like to use a member function of an object as
the function the thread is suppose to execute. So i have this member
function declared like this :
int Decoder::internalLoop(void * data);

and somewhere in another member function of the same object i call :
SDL_CreateThread(this->internalLoop, NULL);

And during compilation, VC just spit that in my face :
“Decoder.cpp(28) : error C2664: ‘SDL_CreateThread’ : cannot convert
parameter 1 from ‘int (void *)’ to ‘int (__cdecl *)(void *)’”

So what’s this (__cdecl *) thing? I know some people have been able to do
just what i’m trying to do with the same kinda call. (see MPEGSystem.cpp in
SMPEG 0.4.4)

Note : OS = win2k and IDE = MS Visual C++ 6…0

thx in advance
Alx

Alexis wrote:

Now i’m coding in C++ and i’d like to use a member function of an
object as the function the thread is suppose to execute. So i have
this member function declared like this :
int Decoder::internalLoop(void * data);

struct thread_data {
void *data;
Decoder *decoder;
};

void thread_function(void *data)
{
thread_data *td = static_cast<thread_data *>(data);
td->decoder->internal_loop(td->data);
}

thread_data td;
td.data = your_data;
td.dceoder = &your_decoder;
SDL_CreateThread(thread_function, static_cast<void *>(&td));

The management of the ‘thread_data’ object is left as an excercise for
the reader.–
Rainer Deyke | root at rainerdeyke.com | http://rainerdeyke.com

Member functions have an implicit parameter, the this-pointer. This is one
of the reasons why pointer-to-members are incompatible with
pointer-to-functions.

To do what you want to do, you need a thunk function that looks like this:

int thunk_internalLoop(void *data)
{
return ((Decoder *)data)->internalLoop();
}

Then call SDL_CreateThread like this:

SDL_CreateThread(thunk_internalLoop, this);

Note that the thunk function can be a static member function as these don’t
have this-pointers.

cu,
NicolaiOn Tuesday 27 August 2002 17:18, Alexis wrote:

Now i’m coding in C++ and i’d like to use a member function of an object
as the function the thread is suppose to execute. So i have this member
function declared like this :
int Decoder::internalLoop(void * data);

and somewhere in another member function of the same object i call :
SDL_CreateThread(this->internalLoop, NULL);

And during compilation, VC just spit that in my face :
“Decoder.cpp(28) : error C2664: ‘SDL_CreateThread’ : cannot convert
parameter 1 from ‘int (void *)’ to ‘int (__cdecl *)(void *)’”

You can’t call a NON-STATIC member of a class via a function pointer.

Try this:

// in .h file

class MyClass
{
int internalLoop(void* /* Not Used */ )

static int startInternalLoop (void* ThisPointer);

}

// In .cpp file

int MyClass::startInternalLoop(void* ThisPointer)
{
MyClass* RealThisPointer = (MyClass*)ThisPointer;
RealThisPointer->internalLoop(NULL);
}

// in your existing function:

int main(void)
{
SDL_CreateThread(&MyClass::startInternalLoop, (void*)this);
}

Hope that helped,

-LorenOn Tue, 2002-08-27 at 08:18, Alexis wrote:

I’m affraid all of this is just a matter of C/C++ conversion stuff, i hope
i’m wrong.
I got to the point where my SDL app has to use multithreading. So I used the
function :
SDL_Thread *SDL_CreateThread(int (*fn)(void *), void *data);
to create a thread in a C program.

Now i’m coding in C++ and i’d like to use a member function of an object as
the function the thread is suppose to execute. So i have this member
function declared like this :
int Decoder::internalLoop(void * data);

and somewhere in another member function of the same object i call :
SDL_CreateThread(this->internalLoop, NULL);

And during compilation, VC just spit that in my face :
“Decoder.cpp(28) : error C2664: ‘SDL_CreateThread’ : cannot convert
parameter 1 from ‘int (void *)’ to ‘int (__cdecl *)(void *)’”

So what’s this (__cdecl *) thing? I know some people have been able to do
just what i’m trying to do with the same kinda call. (see MPEGSystem.cpp in
SMPEG 0.4.4)

Note : OS = win2k and IDE = MS Visual C++ 6…0

Thanks a lot to all your help.
:slight_smile: