Chinaunix首页 | 论坛 | 博客
  • 博客访问: 5632
  • 博文数量: 5
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 60
  • 用 户 组: 普通用户
  • 注册时间: 2015-06-16 15:45
文章分类

全部博文(5)

文章存档

2015年(5)

我的朋友
最近访客

分类: LINUX

2015-06-16 16:20:21

gtk多线程例子,来源于gtk  demo,作者 Erik Mouw

点击(此处)折叠或打开

  1. /*-------------------------------------------------------------------------
  2.  * Filename: gtk_thread.c
  3.  * Version: 0.99.1
  4.  * Copyright: Copyright (C) 1999, Erik Mouw
  5.  * Author: Erik Mouw <J.A.K.Mouw@its.tudelft.nl>
  6.  * Description: GTK threads example.
  7.  * Created at: Sun Oct 17 21:27:09 1999
  8.  * Modified by: Erik Mouw <J.A.K.Mouw@its.tudelft.nl>
  9.  * Modified at: Sun Oct 24 17:21:41 1999
  10.  *-----------------------------------------------------------------------*/
  11. /*
  12.  * Compile with:
  13.  *
  14.  * cc -o gtk_thread gtk_thread.c `gtk-config --cflags --libs gthread`
  15.  *
  16.  * Thanks to Sebastian Wilhelmi and Owen Taylor for pointing out some
  17.  * bugs.
  18.  *
  19.  */
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <unistd.h>
  23. #include <time.h>
  24. #include <gtk/gtk.h>
  25. #include <glib.h>
  26. #include <pthread.h>
  27. #define YES_IT_IS (1)
  28. #define NO_IT_IS_NOT (0)
  29. typedef struct
  30. {
  31.   GtkWidget *label;
  32.   int what;
  33. } yes_or_no_args;
  34. G_LOCK_DEFINE_STATIC (yes_or_no);
  35. static volatile int yes_or_no = YES_IT_IS;
  36. void destroy (GtkWidget *widget, gpointer data)
  37. {
  38.   gtk_main_quit ();
  39. }
  40. void *argument_thread (void *args)
  41. {
  42.   yes_or_no_args *data = (yes_or_no_args *)args;
  43.   gboolean say_something;
  44.   for (;;)
  45.     {
  46.       /* sleep a while */
  47.       sleep(rand() / (RAND_MAX / 3) + 1);
  48.       /* lock the yes_or_no_variable */
  49.       G_LOCK(yes_or_no);
  50.       /* do we have to say something? */
  51.       say_something = (yes_or_no != data->what);
  52.       if(say_something)
  53.     {
  54.       /* set the variable */
  55.       yes_or_no = data->what;
  56.     }
  57.       /* Unlock the yes_or_no variable */
  58.       G_UNLOCK (yes_or_no);
  59.       if (say_something)
  60.     {
  61.       /* get GTK thread lock */
  62.       gdk_threads_enter ();
  63.       /* set label text */
  64.       if(data->what == YES_IT_IS)
  65.         gtk_label_set_text (GTK_LABEL (data->label), "O yes, it is!");
  66.       else
  67.         gtk_label_set_text (GTK_LABEL (data->label), "O no, it isn't!");
  68.       /* release GTK thread lock */
  69.       gdk_threads_leave ();
  70.     }
  71.     }
  72.   return NULL;
  73. }
  74. int main (int argc, char *argv[])
  75. {
  76.   GtkWidget *window;
  77.   GtkWidget *label;
  78.   yes_or_no_args yes_args, no_args;
  79.   pthread_t no_tid, yes_tid;
  80.   /* init threads */
  81.   g_thread_init (NULL);
  82.   gdk_threads_init ();
  83.   gdk_threads_enter ();
  84.   /* init gtk */
  85.   gtk_init(&argc, &argv);
  86.   /* init random number generator */
  87.   srand ((unsigned int) time (NULL));
  88.   /* create a window */
  89.   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  90.   g_signal_connect (window, "destroy", G_CALLBACK (destroy), NULL);
  91.   gtk_container_set_border_width (GTK_CONTAINER (window), 10);
  92.   /* create a label */
  93.   label = gtk_label_new ("And now for something completely different ...");
  94.   gtk_container_add (GTK_CONTAINER (window), label);
  95.   /* show everything */
  96.   gtk_widget_show (label);
  97.   gtk_widget_show (window);
  98.   /* create the threads */
  99.   yes_args.label = label;
  100.   yes_args.what = YES_IT_IS;
  101.   pthread_create (&yes_tid, NULL, argument_thread, &yes_args);
  102.   no_args.label = label;
  103.   no_args.what = NO_IT_IS_NOT;
  104.   pthread_create (&no_tid, NULL, argument_thread, &no_args);
  105.   /* enter the GTK main loop */
  106.   gtk_main ();
  107.   gdk_threads_leave ();
  108.   return 0;
  109. }

阅读(302) | 评论(0) | 转发(0) |
0

上一篇:没有了

下一篇:gtk多线程例子的编译步骤

给主人留下些什么吧!~~