I have made a simple program with two threads.
One thread flashes a button and the other just does a printf ever 100msec.
After some time of flashing of the button I get the error as show below: the code is also show below:
The program 'thread_test' received an X Window System error.
This probably reflects a bug in the program.
The error was 'BadPixmap (invalid Pixmap parameter)'.
(Details: serial 1048 error_code 4 request_code 56 minor_code 0)
(Note to programmers: normally, X errors are reported asynchronously;
that is, you will receive the error a while after causing it.
To debug your program, run it with the --sync command line
option to change this behavior. You can then get a meaningful
backtrace from your debugger if you break on the gdk_x_error() function.)
Code:
// gcc -o thread_test -g button_flash.c `pkg-config --cflags --libs gtk+-2.0`
#include <gtk/gtk.h>
typedef struct
{
GtkWidget * winx;
GtkWidget * button;
GdkColor Colors[2];
} xwindow;
xwindow * tw;
gint fast_thread()
{
gint t;
t = 0;
for(;;)
{
g_usleep(100 * 1000);
printf("FAST %d\n", t);
t ++;
}
return 0;
}
gint slow_thread()
{
gboolean phase;
phase = FALSE;
for(;;)
{
g_usleep(250 * 1000);
gdk_threads_enter();
if(phase == FALSE)
{
phase = TRUE;
gtk_widget_modify_bg (tw->button, GTK_STATE_NORMAL, &tw->Colors[0]);
gtk_widget_modify_bg (tw->button, GTK_STATE_PRELIGHT, &tw->Colors[0]);
}
else
{
phase = FALSE;
gtk_widget_modify_bg (tw->button, GTK_STATE_NORMAL, &tw->Colors[1]);
gtk_widget_modify_bg (tw->button, GTK_STATE_PRELIGHT, &tw->Colors[1]);
}
gdk_threads_leave();
}
return 0;
}
static gint i;
gint button_click(GtkWidget *button, gint data)
{
for ( i = 0 ; i < 10 ; i ++)
{
gtk_button_set_label (GTK_BUTTON(tw->button), "set");
g_usleep(1000 * 1000);
}
return 0;
}
gint main (gint argc, gchar *argv[])
{
gtk_init (&argc, &argv);
tw = g_slice_new (xwindow);
// build window
gdk_color_parse ("#000000", &tw->Colors[0]);// black
gdk_color_parse ("#ffffff", &tw->Colors[1]);// white
tw->winx = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_widget_set_size_request (tw->winx, 200, 100);
g_signal_connect (tw->winx, "destroy",G_CALLBACK (gtk_main_quit), NULL);
tw->button = gtk_button_new();
gtk_container_add (GTK_CONTAINER(tw->winx), tw->button);
g_signal_connect (G_OBJECT (tw->button), "clicked", G_CALLBACK (button_click), NULL);
gtk_widget_show_all(tw->winx);
GError *err1, *err2;
if(!g_thread_supported())
{
g_thread_init(NULL);
gdk_threads_init();
}
else
{
}
gtk_init (&argc, &argv);
if(!g_thread_create((GThreadFunc)fast_thread, NULL, FALSE, &err1))
{
g_print("FAST Thread create failed: %s!!\n", err1->message );
g_error_free ( err1 ) ;
}
else
{
g_print(" vvvvv FAST vvvvv\n");
}
if(!g_thread_create((GThreadFunc)slow_thread, NULL, FALSE, &err2))
{
printf("SLOW Thread create failed: %s!!\n", err2->message );
g_error_free ( err2 ) ;
}
else
{
g_print(" vvvvv SLOW vvvvv\n");
}
gdk_threads_enter();
gtk_main ();
gdk_threads_leave();
g_slice_free (xwindow, tw);
return 0;
}
I have not been able to get any specific solutions for this.
Thanks in advance.