TA的每日心情 | 奋斗 2016-10-11 09:20 |
---|
签到天数: 271 天 连续签到: 1 天 [LV.8]以坛为家I
|
转自:http://blog.csdn.net/c_spark/article/details/4742919
1、初始化gthread
if (!g_thread_supported ()) //如果gthread没有被初始化
g_thread_init (NULL); //进行初始化
gdk_threads_init (); //初始化GDK多线程,这样可以在多线程中使用成对的gdk_threads_enter()和gdk_thread_leave(),在Gtk程序保证 gdk_threads_init()在main loop执行之前执行,为了保证线程安全应该在gtk_init()之前调用,g_thread_init()必须在函数gdk_threads_init()之前执行。
2、gdk_threads_enter()和gdk_threads_leave()
gdk_threads_enter()这是一宏定义,占领临界区,这样使Gdk和Gtk+函数在多线程中被安全的调用而不会造成竞争条件,保证数据安全,一次只能有一线程访问临界区。gdk_threads_leave释放对当前临界区访问。
在调用任何与gtk相关函数时,需要使用gdk_threads_enter()和gdk_threads_leave()成对的宏,保证当前函数的的调用只可能在一个线程中。
3、示例如下:
- #include <glib.h>
- #include <gtk/gtk.h>
- #include <libgnomevfs/gnome-vfs.h>
- #define BUF_SIZE 4096
- GtkWidget *window;
- GtkWidget *image = NULL;
- GtkWidget *vbox = NULL;
- const gchar *uri = "/home/cspark/logo-large.png";
- void callback (void)
- {
- g_print ("%d %s/n", __LINE__, __FUNCTION__);
- }
-
- void
- thread_func (GtkWidget *button)
- {
- int i;
- for (i = 0; i <= 20; i++)
- {
- sleep (1);
- gchar *s = g_strdup_printf ("Test %d", i);
- gdk_threads_enter ();
- gtk_button_set_label (GTK_BUTTON (button), s);
- gdk_threads_leave ();
- if (s) {
- g_free (s);
- s = NULL;
- }
- }
- }
- void
- load_image ()
- {
- GnomeVFSHandle *handle;
- GnomeVFSResult result;
- GdkPixbufLoader *loader;
- GdkPixbuf *pixbuf;
- gchar buffer[BUF_SIZE];
- GnomeVFSFileSize bytes_read;
- result = gnome_vfs_open (&handle, uri, GNOME_VFS_OPEN_READ);
-
- loader = gdk_pixbuf_loader_new ();
-
- while (result == GNOME_VFS_OK) {
- result = gnome_vfs_read (handle, buffer,
- BUF_SIZE, &bytes_read);
- gdk_pixbuf_loader_write (loader, buffer, bytes_read, NULL);
- sleep (2);
- }
- gdk_pixbuf_loader_close (loader, NULL);
- pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
-
- if (!pixbuf) {
- g_print ("pixbuf is NULl");
- return;
- }
- gdk_threads_enter ();
- image = gtk_image_new_from_pixbuf (pixbuf);
- gtk_box_pack_start (GTK_BOX (vbox), image, FALSE, FALSE, 0);
- gtk_widget_show (image);
- gdk_threads_leave ();
- }
- int main (int argc, char *argv[])
- {
- if (!g_thread_supported ())
- g_thread_init (NULL);
- gdk_threads_init();
- gdk_threads_enter ();
- gtk_init (&argc, &argv);
-
- gnome_vfs_init ();
-
- window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
-
- g_signal_connect (window, "destroy", gtk_main_quit, NULL);
- GtkWidget *button = NULL;
- button = gtk_button_new_with_label ("Test Mutilate GThread");
- vbox = gtk_vbox_new (FALSE, 0);
- gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);
- g_signal_connect (button, "clicked", (GCallback)(callback), NULL);
- gtk_widget_show (button);
- gtk_container_add (GTK_CONTAINER (window), vbox);
- gtk_widget_show_all (window);
-
- g_thread_create ((GThreadFunc) (load_image), NULL, FALSE, NULL);
- g_thread_create ((GThreadFunc) (thread_func), button, FALSE, NULL);
- gtk_main ();
- gdk_threads_leave ();
- return 0;
- }
复制代码
Makefile:
view plain
- CC=gcc
- CFLAGS = `pkg-config --cflags gtk+-2.0 gthread-2.0 gnome-vfs-2.0` -g
- LDFLAGS = `pkg-config --libs gtk+-2.0 gthread-2.0 gnome-vfs-2.0` -rdynamic
- SRC=gnomevfs-test.c
- test:$(SRC)
- $(CC) $(CFLAGS) $(LDFLAGS) $(SRC) -o test
复制代码 |
|