Re: Multi-threaded GUI



Pthreads can't call directly into C++ code.

It expects a function like:

void* (fn*) ( void* arg ) ;

Putting this in a class gives something like:

void* (Class::fn*) (void* arg)

Plus you're missing the this pointer and all sorts of other things.

The way to get around this is to either use a thread library like Glibmm or boost::thread or do something like this:

class MyClass ;

void* jump_start( void* myclass_this_ptr )
{
    MyClass* obj = dynamic_cast< MyClass* >( myclass_this_ptr ) ;

    if( !obj )
       error_message( "Wrong type of pointer passed to function" ) ;
     else
       obj->run() ;
}

MyClass
{
   public:
      void run()
     {
         /* Do something useful */
      }

      void start()
      {
          pthread_t tid ;

          /* This is off the top of my head, so it may be wrong.
             but i think its, pthread_create( pthread_t*, pthread_attr_t*, function*, arg* ) */
          pthread_create( &tid,  NULL,  &start_wrapper,  this ) ;
      }
}

I'm pretty sure that this is more or less what all the threads libraries do.

Hope that helps.

Paul





On 10/4/06, Melvin Newman <zexelon gmail com> wrote:
My saga towards updating the GUI continues... the following are two code snippets:

1) My window update function. It is built into the window1 class and is supposed to update the main GUI window:

gboolean window1::updater(gpointer data)
{
    /* Variables
    ******************************************/
        Glib::RefPtr<Gdk::Window>win = get_window();
    /*****************************************/
   
    if(win)
    {
        Gdk::Rectangle r(0, 0, get_allocation().get_width(),get_allocation().get_height());
        win->invalidate_rect(r, false);
    }
    return(true);
}


2) The following is my thread that calls this function through the g_idle_add() function... Unfortunately it does not compile at this line and being new to OO/C++ I have no idea why, or how to fix the problem:

void *server_connect(void *ptr)
{
    /* Variables
    ******************************************/
        int tmp=0;
        int socket_id;
        struct sockaddr_in address;
        gpointer data;
        Glib::RefPtr<Gtk::TextBuffer> buffer;
        sigc::connection pipe1;
    /*****************************************/
   
    socket_id = pthread_arg.socket_id;
    address = pthread_arg.address;
    buffer = pthread_arg.out_put_buffer;
       
    do
    {
        tmp = connect (socket_id,(struct sockaddr *) &address, sizeof(struct sockaddr));
        if (tmp <0)
        {
            buffer->insert_at_cursor("@");
           
            g_idle_add(window1::updater,false);
               ^-- Compiling stops here with: error: invalid use of non-static member function `gboolean window1::updater(void*)'
           
            Glib::usleep(400000);
        }
       
    }while(tmp<0);
   
}

Unfortunately I cant seem to integrate the server_connect function into the class, because pthread then complains that it can not call it (miss mach type).

Any help would be greatly appreciated.

Sincerely
Melvin Newman

_______________________________________________
gtk-list mailing list
gtk-list gnome org
http://mail.gnome.org/mailman/listinfo/gtk-list





[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]