threads problem with Class

View: New views
9 Messages — Rating Filter:   Alert me  

threads problem with Class

by Leonel Florín Selles :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


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@...
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


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

Re: threads problem with Class

by drblitzkrieg :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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 <leonel06033@...>

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@...
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


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


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

Re: threads problem with Class

by Kenneth Bull :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

2009/10/27 Leonel Florín Selles <leonel06033@...>:
> 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@...
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

threaddemo.cpp (8K) Download Attachment

Re: threads problem with Class

by Brian Barrett :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Wed, Oct 28, 2009 at 9:21 PM, Stephan Sauerburger
<drblitzkrieg@...> wrote:
> Use C instead. It's faster anyway.

Absolutely false, and typically irrelevant even if it were true, due
to the 80-20 rule.
_______________________________________________
SDL mailing list
SDL@...
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Re: threads problem with Class

by Kenneth Bull :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

2009/10/29 Brian <brian.ripoff@...>:
> On Wed, Oct 28, 2009 at 9:21 PM, Stephan Sauerburger
> <drblitzkrieg@...> 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.
_______________________________________________
SDL mailing list
SDL@...
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Re: threads problem with Class

by Brian Barrett :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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++.

-- Brian

On Thu, Oct 29, 2009 at 9:00 AM, Kenneth Bull <llubnek@...> wrote:

> 2009/10/29 Brian <brian.ripoff@...>:
>> On Wed, Oct 28, 2009 at 9:21 PM, Stephan Sauerburger
>> <drblitzkrieg@...> 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.
_______________________________________________
SDL mailing list
SDL@...
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Re: threads problem with Class

by Kenneth Bull :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

2009/10/29 Kenneth Bull <llubnek@...>:
> 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.


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

threaddemo.cpp (8K) Download Attachment

R: Re: threads problem with Class

by Abderrahmane Madani-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Some parts of this message have been removed. Learn more about Nabble's security policy.
Or make _thread_runner a static member function of Thread class.

2009/10/27
Leonel Florín Selles <leonel06033@...>:
> 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@...
http://lists.libsdl.org/listin

----- Messaggio originale -----
Da: "Kenneth Bull" <llubnek@...>
A: "SDL Development List" <sdl@...>
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@... http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

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

Re: R: Re: threads problem with Class

by Kenneth Bull :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

what's your question?
_______________________________________________
SDL mailing list
SDL@...
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org