Re: [gtk-list] pthreads and gtk 1.2



On Thu, 07 Oct 1999 17:29:26 +0200, WPD mestrole wrote:
> I want to grab stdout and stderr to display it in a GtkText.
> I want to be able to do it even if the gtk_main loop is deactivated
> (so I think I can't use gtk_timout_add to do it)

Why don't you just make your own printf functions that does something
like:

  int my_fprintf(FILE* f, char *str, ...)
  {
    va_list argp;
    char buf[256];
    int written = 0;

    va_start(argp, str);

    if((f == stdout) || (f == stderr))
      {
        written = vsnprintf(buf, 256, str, argp);

        /* gtk code to set correct widget */
      }
    else
     {
       written = vfprintf(f, str, argp);
     }
        
    va_end(argp);
  
    return(written);
  }

Search and replace each fprintf with my_fprintf and you're done (without
threads).

> a piece of my code :
> 
> GtkWidget * text;
> GtkWidget * window;
> 
> void * Thr (*void)
> {
> char  buf_err[BUFSIZ];
> char  buf_out[BUFSIZ];
> char B[BUFSIZ];
> int n;
> 
> setbuf(stdout,buf_out);
> setbuf(stderr,buf_err);
> 
> while(1)
>     {
>     if (buf_out[0]!=0)
>         {
>         fflush(stdout);
>         sprintf(B,"%s",buf_out);
>         n=0;
>         while(B[n]!=0){
> gtk_text_insert(GTK_TEXT(text),NULL,NULL,NULL,B+n++,1);}
>         }
>         if (buf_err[0]!=0)
>         {
>         fflush(stderr);
>         sprintf(B,"%s",buf_err);
>         n=0;
>         while(B[n]!=0){
> gtk_text_insert(GTK_TEXT(text),NULL,NULL,NULL,B+n++,1);}
>         }
>   usleep(10000);
>    }
> return NULL;
> }
> 
> 
>   int main(void)
> {
> pthread_t  pth;
> int H;
> 
> ... creation of the window and the gtktext...
> gtk_widget_show(window);
> 
> pthread_create(&th,NULL,(void*)Thr,*void);
> 
>     printf(" 1st section\n");
> 
> H=gtk_timeout_add(.... // display values of some external captors
> 
>     printf(" 2nd section\n");
> ...
> }
> 
> but it doesn't react very well: everything is blocked before displaying
> the window with this code.
> If I remove the setbuf(stderr,...) I can see 1st section on the terminal
> 
> and on the gtktext, but 2nd section only on the terminal... But not
> everytime... and I get sometimes Xlib message : " sync failed ".

Sounds like two threads using the X connection at the same time.

> If I use this code replacing gtktext by a redirection to a logfile,
> everything is correct.

Correct, because in that case your thread doesn't mess with GTK's X
connection.

> (the pthread how-to says : with linux <2.1.x, Xlib use the two SIGUSR,
> so can't use pthreads; but I do it with RTlinuxV2b12 base on kernel
> 2.2.10 that have much more free SIGUSR! don't understand...)

Hmm, AFAIK you don't need RTlinux for doing pthreads.

> How could I get a stable processus, something doing what I want
> everytime??

You need to tell GTK to be thread aware. There are some special functions
for that, see question 5.2 of the GTK FAQ at
http://www.gtk.org/faq/gtkfaq-5.html#ss5.2 .


Erik

-- 
J.A.K. (Erik) Mouw, Information and Communication Theory Group, Department
of Electrical Engineering, Faculty of Information Technology and Systems,
Delft University of Technology, PO BOX 5031,  2600 GA Delft, The Netherlands
Phone: +31-15-2785859  Fax: +31-15-2781843  Email J.A.K.Mouw@its.tudelft.nl
WWW: http://www-ict.its.tudelft.nl/~erik/




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