[cairo-commit] [cairo-www] src/threaded_animation_with_cairo.mdwn
Carl Worth
cworth at freedesktop.org
Sat Feb 9 19:04:56 PST 2008
src/threaded_animation_with_cairo.mdwn | 19 +++++++++++++++++--
1 file changed, 17 insertions(+), 2 deletions(-)
New commits:
commit 169ecb0cba145e52b11caef3cb6aed044ee49405
Author: Carl Worth <cworth at freedesktop.org>
Date: Sat Feb 9 19:04:55 2008 -0800
web commit by Albert
diff --git a/src/threaded_animation_with_cairo.mdwn b/src/threaded_animation_with_cairo.mdwn
index 53188b1..ac06f05 100644
--- a/src/threaded_animation_with_cairo.mdwn
+++ b/src/threaded_animation_with_cairo.mdwn
@@ -100,6 +100,8 @@ Before, we implement our timer function, we must consider one important issue.
gboolean timer_exe(GtkWidget * window){
+ static gboolean first_execution = TRUE;
+
//use a safe function to get the value of currently_drawing so
//we don't run into the usual multithreading issues
int drawing_status = g_atomic_int_get(¤tly_drawing);
@@ -107,8 +109,11 @@ Before, we implement our timer function, we must consider one important issue.
//if we are not currently drawing anything, launch a thread to
//update our pixmap
if(drawing_status == 0){
- pthread_t thread_info;
+ static pthread_t thread_info;
int iret;
+ if(first_execution != TRUE){
+ pthread_join(thread_info, NULL);
+ }
iret = pthread_create( &thread_info, NULL, do_draw, NULL);
}
@@ -117,6 +122,8 @@ Before, we implement our timer function, we must consider one important issue.
gdk_drawable_get_size(pixmap, &width, &height);
gtk_widget_queue_draw_area(window, 0, 0, width, height);
+ first_execution = FALSE;
+
return TRUE;
}
@@ -124,6 +131,7 @@ Before, we implement our timer function, we must consider one important issue.
* `g_atomic_int_get(¤tly_drawing)` is a thread-safe way to get the value of our global integer `currently_drawing`. Using this function allows us to avoid the possibility of reading a number at the same moment our other thread is trying to change it. It is also much easier to implement than mutexes for reading a single integer.
* `pthread_create( &thread_info, NULL, do_draw, NULL)` is the `unistd.h` way to launch the function `do_draw()` as a separate thread. (The final `NULL` is actually a `(void *)` to a data structure that we pass to `do_draw()`.)
* `gtk_widget_queue_draw_area(window, 0, 0, width, height)` sends an artificial expose event with upper left corner 0,0 and width and heigh of `width`, `height`, respectively. This allows our `expose_event` to do the actual painting.
+ * `pthread_join(thread_info, NULL)` re-joins the drawing thread and ensures that it terminates and it's OS-related memory is freed.
##Do the Drawing
@@ -323,6 +331,8 @@ For your compiling pleasure, the full source in proper order.
gboolean timer_exe(GtkWidget * window){
+ static gboolean first_execution = TRUE;
+
//use a safe function to get the value of currently_drawing so
//we don't run into the usual multithreading issues
int drawing_status = g_atomic_int_get(¤tly_drawing);
@@ -330,8 +340,11 @@ For your compiling pleasure, the full source in proper order.
//if we are not currently drawing anything, launch a thread to
//update our pixmap
if(drawing_status == 0){
- pthread_t thread_info;
+ static pthread_t thread_info;
int iret;
+ if(first_execution != TRUE){
+ pthread_join(thread_info, NULL);
+ }
iret = pthread_create( &thread_info, NULL, do_draw, NULL);
}
@@ -340,6 +353,8 @@ For your compiling pleasure, the full source in proper order.
gdk_drawable_get_size(pixmap, &width, &height);
gtk_widget_queue_draw_area(window, 0, 0, width, height);
+ first_execution = FALSE;
+
return TRUE;
}
More information about the cairo-commit
mailing list