Threads and C++

Hello,

My questions may seem silly but I was wondering how I could use threads with
C++, i.e., by using a member function in SDL_CreateThread().

For example :

int Class::Function(void *nothing)
{
return 0;
}

SDL_CreateThread(Class::Function, NULL);

I know this doesn’t work, but is there a work-around ? Using an intermediary
non-member function with (this) as parameter ?

Thanks,

Damiano

The problem is that SDL_CreateThread must call a normal
function and not a method.

A solution is to have a function that works as a proxy
for your thread class.

For example,

Thread::Thread ()
{
SDL_CreateThread (run_thread, this);
}

void Thread::Run ()
{
// Thread main loop
}

void run_thread(void *data)
{
Thread thr = static_cast<Thread>(data); // Or a reinterpret_cast<>
not sure
thr->Run ();
}

Of course run_thread() can also be coded as a static member function.

Hope this helps,
Paulo Pinto

Damiano wrote:>Hello,

My questions may seem silly but I was wondering how I could use threads with
C++, i.e., by using a member function in SDL_CreateThread().

For example :

int Class::Function(void *nothing)
{
return 0;
}

SDL_CreateThread(Class::Function, NULL);

I know this doesn’t work, but is there a work-around ? Using an intermediary
non-member function with (this) as parameter ?

Thanks,

Damiano


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

The simplest method i guess is using a proxi function.
I think its interesting to say there is a lib called Adaptive
communication Enviroment (ACE) that is a c++ toolkit over

signals
times
threards
memory management
IPC
Pipes
Sockets

In the area of threads, it has the function thread and the
implementation of two pattern based objects, “task” and “active object”

The task is a thread object derived of the class ACE_Task. To use it
you have to subclass the task class and implement three methods:

class::open(); class::close();
to initialice and clean your thread enviroment.

class::svc(); // or Servece Procesing Method
it is used as entry point.

then

myclass mythread;
mythread.open();
mythread.active(num_of_threads);

it also implements a mesage queue, one for each thread, to allow the
threads comunicate between them.

The other pattern is the active object. In this, all methods calling
are non block calls (all the object run in a thread, and all the calls
are done async). It hard to explain it so if you wanna know, it is open
source and has a page on the net, but i just dont have it here.

Other posibility is to implemen the task base class yourself, i think
it is not so hard and even it may encapsulate a proxy function.

Wish to be usefull

Losky

El 2003.08.14 07:52, Damiano escribi?:> Hello,

My questions may seem silly but I was wondering how I could use
threads with
C++, i.e., by using a member function in SDL_CreateThread().

For example :

int Class::Function(void *nothing)
{
return 0;
}

SDL_CreateThread(Class::Function, NULL);

I know this doesn’t work, but is there a work-around ? Using an
intermediary
non-member function with (this) as parameter ?

Thanks,

Damiano


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

Ok, thanks for all guys, it worked using a proxy function.

Damiano