Re: gtk_timeout_add / SIGALRM with gtk
- From: Colin Thomas <colin in2fab com>
- To: taf2 lehigh edu, gtk-app-devel-list gnome org
- Cc: colin in2fab com
- Subject: Re: gtk_timeout_add / SIGALRM with gtk
- Date: 28 May 2004 10:13:06 +0100
Hi Todd,
Apologises for the delay in getting back, but I have been profiling some
code and trying to speed up a routine that is called 1.3 billion times.
What I have done is take you ideas and create a thread to periodically
refresh the main window, which is started and stopped on the enter and
exit of the button being depressed.
i.e.
1. Call back for button being depressed:
========================================
void
on_button_clicked (GtkButton *button,
gpointer user_data)
{
////////////////////////////////////////
g_mutex_lock(lock);
//Set global flag to mark thread running
working = true;
g_mutex_unlock( lock);
//Start the thread
tid = g_thread_create((GThreadFunc) RunThread, NULL , FALSE , NULL);
////////////////////////////////////////
//Large loop to burn cpu time
unsigned int count = 0;
for (
count = 0;
count < 100000000;
count++
)
{
}
////////////////////////////////////////
//job finished, inform thread to kill itself
g_mutex_lock(lock);
working = false;
g_mutex_unlock( lock);
//Sleep for the same amount of time as the thread refresh time, to
//ensure that the thread sees working as false and dies, before a
//possible 2nd thread is created, and two threads end up running in
//parallel.
g_usleep(500000);
////////////////////////////////////////
}
2. Function to refresh the main window:
=======================================
void RunThread ()
{
//Thread will die when work set to false (when main job completes)
while(working)
{
gtk_widget_draw (pTOPWindow,NULL);
gdk_flush();
//put thread to sleep for 1/2 sec => refresh every 1/2 sec
g_usleep(500000);
}
}
3. Enabling code in main routine:
=================================
int
main (int argc, char *argv[])
{
g_thread_init(NULL);
gdk_threads_init();
gtk_init(&argc, &argv );
lock = g_mutex_new();
.
.
.
This seems to be okay.. can you see any possible flaws?
Many thanks again for your help.
Best regards
Colin Thomas
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]