Threads problem with Class

hello again friends:

well now I have a problem with threads, the point is that I have a class that
handles a thread, and when I send to the function SDL_Thread
*SDL_CreateThread(int (*fn)(void *), void *data); his first argument that is the
funtion that I want as a threads I get an error message, telling me this

error: argument of type ‘int (Cpacman::)(void*)’ does not match ‘int ()(void)’

I understand what means that message, but i don’t know how to made the
conversion, because the funtion that I’m sending is a class method and
SDL_CreateThread need a function, not a method of a class, I don’t know if I
make myself clear.

but if anyone knows what I mean, please tell me how to solve this problem.

greetings._______________________________________________
SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Use C instead. It’s faster anyway.
Rather than a class with methods:
myclass.dosomething(int a,int b,int c);
use a structure and functions that take a pointer to it as an arg:
dosomething(struct myStruct *s,int a,int b,int c);

2009/10/27 Leonel Flor?n Selles >

hello again friends:

well now I have a problem with threads, the point is that I have a class
that
handles a thread, and when I send to the function SDL_Thread
*SDL_CreateThread(int (*fn)(void *), void *data); his first argument that
is the
funtion that I want as a threads I get an error message, telling me this

error: argument of type ‘int (Cpacman::)(void*)’ does not match ‘int
()(void)’

I understand what means that message, but i don’t know how to made the
conversion, because the funtion that I’m sending is a class method and
SDL_CreateThread need a function, not a method of a class, I don’t know if
I
make myself clear.

but if anyone knows what I mean, please tell me how to solve this problem.

greetings.


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

2009/10/27 Leonel Flor?n Selles :

error: argument of type ‘int (Cpacman::)(void*)’ does not match ‘int ()(void)’

Unfortunately, you can’t use a C++ member function with SDL_CreateThread().
You need to do something like this:

#include “SDL.h”

class Thread {
friend int _thread_runner(void*);
public:
Thread();
virtual ~Thread();

    void start();
    int wait();
    void kill();

protected:
    int _run();
    virtual int run();
    SDL_Thread* thread;

};

Thread::Thread() {
thread = 0;
}

Thread::~Thread() {
kill();
}

void Thread::start() {
if (thread) return;
thread = SDL_CreateThread(_thread_runner, this);
}

int Thread::wait() {
int ret = 0;
if (!thread) return 0;
SDL_WaitThread(thread, &ret);
return ret;
}

void Thread::kill() {
if (!thread) return;
SDL_KillThread(thread); // does nothing in SDL 1.3
thread = 0;
}

int Thread::_run() {
int ret = run();
thread = 0;
return ret;
}

int Thread::run() {
return 0;
}

int _thread_runner(void* data) {
return reinterpret_cast<Thread*>(data)->_run();
}

See attached for a timer thingy (not tested with SDL 1.3, works on 1.2).
-------------- next part --------------
A non-text attachment was scrubbed…
Name: threaddemo.cpp
Type: application/octet-stream
Size: 6479 bytes
Desc: not available
URL: http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20091029/e8dc693b/attachment.obj

Absolutely false, and typically irrelevant even if it were true, due
to the 80-20 rule.On Wed, Oct 28, 2009 at 9:21 PM, Stephan Sauerburger wrote:

Use C instead. It’s faster anyway.

2009/10/29 Brian <brian.ripoff at gmail.com>:> On Wed, Oct 28, 2009 at 9:21 PM, Stephan Sauerburger wrote:

Use C instead. It’s faster anyway.

Absolutely false, and typically irrelevant even if it were true, due
to the 80-20 rule.

Not really false. Virtual function tables, RTTI, exceptions, etc. can
slow things down and produce bigger binaries than the equivalent in C.

Often irrelevant, since most programs will spend most of their time
waiting for user input. Not so much with games or multimedia though,
which is where SDL is typically used.

I’d still recommend C++ anyway, because OOP in C is generally ugly and fragile.

Arg. I always tell myself not to reply to those kind of statements. I
should have said something like this:

If thread creation is your bottleneck, you have bigger problems than C
versus C++.

– BrianOn Thu, Oct 29, 2009 at 9:00 AM, Kenneth Bull wrote:

2009/10/29 Brian <@Brian_Barrett>:

On Wed, Oct 28, 2009 at 9:21 PM, Stephan Sauerburger wrote:

Use C instead. It’s faster anyway.

Absolutely false, and typically irrelevant even if it were true, due
to the 80-20 rule.

Not really false. ?Virtual function tables, RTTI, exceptions, etc. can
slow things down and produce bigger binaries than the equivalent in C.

Often irrelevant, since most programs will spend most of their time
waiting for user input. ?Not so much with games or multimedia though,
which is where SDL is typically used.

I’d still recommend C++ anyway, because OOP in C is generally ugly and fragile.

2009/10/29 Kenneth Bull <@Kenneth_Bull>:

See attached for a timer thingy (not tested with SDL 1.3, works on 1.2).

Corrected the 1.3 code. Now it looks like it’s supposed to. Tested
with Sam’s 1.3 snapshot.
-------------- next part --------------
A non-text attachment was scrubbed…
Name: threaddemo.cpp
Type: application/octet-stream
Size: 6584 bytes
Desc: not available
URL: http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20091029/95816888/attachment.obj

Or make _thread_runner a static member function of Thread class.

2009/10/27 Leonel Flor?n Selles < leonel06033 at cfg.jovenclub.cu >:

error: argument of type ‘int (Cpacman::)(void*)’ does not match ‘int ()(void)’

Unfortunately, you can’t use a C++ member function with SDL_CreateThread().
You need to do something like this:

#include “SDL.h”

class Thread ? ?{
friend int _thread_runner(void*);
public:
Thread();
virtual ~Thread();

void start();
int wait();
void kill();

protected:
int _run();
virtual int run();
SDL_Thread* thread;
};

Thread::Thread() ? ?{
thread = 0;
}

Thread::~Thread() ? {
kill();
}

void Thread::start() ? ?{
if (thread) return;
thread = SDL_CreateThread(_thread_runner, this);
}

int Thread::wait() ?{
int ret = 0;
if (!thread) return 0;
SDL_WaitThread(thread, &ret);
return ret;
}

void Thread::kill() {
if (!thread) return;
SDL_KillThread(thread); // does nothing in SDL 1.3
thread = 0;
}

int Thread::_run() {
int ret = run();
thread = 0;
return ret;
}

int Thread::run() ? {
return 0;
}

int _thread_runner(void* data) ?{
return reinterpret_cast<Thread*>(data)->_run();
}

See attached for a timer thingy (not tested with SDL 1.3, works on 1.2)._______________________________________________
SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listin
----- Messaggio originale -----
Da: “Kenneth Bull”
A: “SDL Development List”
Inviato: Gioved?, 29 ottobre 2009 9:42:23 GMT +01:00 Amsterdam/Berlino/Berna/Roma/Stoccolma/Vienna
Oggetto: Re: [SDL] threads problem with Class

2009/10/27 Leonel Flor?n Selles : > error: argument of type ‘int (Cpacman::)(void*)’ does not match ‘int ()(void)’ Unfortunately, you can’t use a C++ member function with SDL_CreateThread(). You need to do something like this: #include “SDL.h” class Thread { friend int _thread_runner(void*); public: Thread(); virtual ~Thread(); void start(); int wait(); void kill(); protected: int _run(); virtual int run(); SDL_Thread* thread; }; Thread::Thread() { thread = 0; } Thread::~Thread() { kill(); } void Thread::start() { if (thread) return; thread = SDL_CreateThread(_thread_runner, this); } int Thread::wait() { int ret = 0; if (!thread) return 0; SDL_WaitThread(thread, &ret); return ret; } void Thread::kill() { if (!thread) return; SDL_KillThread(thread); // does nothing in SDL 1.3 thread = 0; } int Thread::_run() { int ret = run(); thread = 0; return ret; } int Thread::run() { return 0; } int _thread_runner(void* data) { return reinterpret_cast(data)->_run(); } See attached for a timer thingy (not tested with SDL 1.3, works on 1.2).
_______________________________________________ SDL mailing list SDL at lists.libsdl.org http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

what’s your question?