How to use thread???



Hello All,
I tried to create a thread in a simple gtk program but I am getting an error....
Actually wat I wanted to do is that , I want some rectangles to be drawn side by side in a plot area but wid a time  gap of 1 second each, so that we can see actual simulation of drawing.. first i simply put a sleep() function in my code but that didnt wrk rather program run for few seconds without drawing anything and then drew all rectangles alltogether.... now i am trying to create a thread so that this thread can run in background and can print rectangles after 1 sec. each but i am getting an error..... my code is below, someone please help me in this regard as soon as possible ,its very urgent for my academic project...


/**************************CODE*************************************/

#include <gtk/gtk.h>
#include <glib.h>


#define NO_PADDING 0
#define NO_SPACING 0
#define width 40
#define height 40



char val[20];




char *inttostr(int num)
{
int i;
int j;
char ch;
int dig;
i = 0;
if(num == 0)
{
val[0] = '0';
val[1] = '\0';
return val;
}
while(num != 0)
{
dig = num%10;
val[i] = dig + '0';
num = (num - dig)/10;
i++;
}
val[i] = '\0';
for(j = i - 1, i = 0; i < j; j--, i++)
{
ch = val[i];
val[i] = val[j];
val[j] = ch;
}
return val;
}



/* Callbacks */

/**************************************************************************/


int draw_array( GtkWidget *where_to_draw )
{
 gint count_size;
 gint x = 40,y = 120;
 GdkGC *array_gc;
 array_gc = where_to_draw->style->black_gc;
 gchar *size,*dstype;
 
 gdk_draw_string(where_to_draw->window,where_to_draw->style->font, array_gc,10,20,"ARRAY");
 gdk_draw_string(where_to_draw->window,where_to_draw->style->font, array_gc,10,35,"INTEGER");
 
 for(count_size=0;count_size<6;count_size++) {
 gdk_draw_rectangle(where_to_draw->window,array_gc,FALSE,x,y,width,height);
  //sleep(1);
 gdk_draw_string(where_to_draw->window,where_to_draw->style->font, array_gc,x+width/2,y+60,inttostr(count_size));
 x+=width;
 }
 return ( TRUE );
 }



void print_screen(GtkWidget *widget,GtkWidget *where_to_draw )
{
  //draw_array(where_to_draw);
  g_thread_create(draw_array, where_to_draw,FALSE, NULL);
 
}

/*------------------------------------------------------------------------*/
void print_and_quit( GtkButton *was_clicked, gpointer user_data )
{
 /* Use glibs printf equivalent to print a message */
  g_print( "Thank you for using this program.\n");
  gtk_main_quit();
}

gboolean delete_event_handler( GtkWidget *widget, GdkEvent *event,
                              gpointer user_data )
{
   /* Received closure from the window manager */
   g_print("The window manager is asking to close this application\n");
   return( FALSE );  /* FALSE - do not prevent closure */
}





/******************************************************/



int main( int argc, char *argv[] )
{
 GtkWidget *top_widget;
 GtkWidget *vbox, *menu_bar, *file_item, *file_menu, *exit_item;
 GtkWidget *plot_area;
 GtkWidget *plot_item;
 GtkWidget *help_item,*array1D;
 

  /* 1. Initialize the environment */
   gtk_init( &argc, &argv );

 /* 2a. Create widgets */
   top_widget = gtk_window_new( GTK_WINDOW_TOPLEVEL );
   vbox = gtk_vbox_new( FALSE, NO_SPACING );
   menu_bar = gtk_menu_bar_new( );

   /* Create the File item and its menu */
   file_item = gtk_menu_item_new_with_label( "File" );
   file_menu = gtk_menu_new();
   array1D   = gtk_menu_item_new_with_label("Array");
   exit_item = gtk_menu_item_new_with_label( "Exit" );

  /* Create the Plot item */
   plot_item = gtk_menu_item_new_with_label( "Plot" );

  /* Create the Help item */
   help_item = gtk_menu_item_new_with_label( "Help" );

  /* Create the Work Area */
   plot_area = gtk_drawing_area_new();

 /* 2b. Set Attributes */
   gtk_window_set_title( GTK_WINDOW( top_widget ), "DSDe" );
   gtk_drawing_area_size( GTK_DRAWING_AREA(plot_area), 800, 400 );

 /* 3. Registering Callback routines */
   gtk_signal_connect( GTK_OBJECT( top_widget ), "delete_event",
                       GTK_SIGNAL_FUNC(delete_event_handler), NULL );
   gtk_signal_connect( GTK_OBJECT( top_widget ), "destroy",
                       GTK_SIGNAL_FUNC(print_and_quit), NULL );
   gtk_signal_connect( GTK_OBJECT( exit_item ), "activate",
                       print_and_quit, NULL );
  
   gtk_signal_connect( GTK_OBJECT( array1D ), "activate",
                       print_screen, plot_area );

 /* 4. Defining the Instance Hierarchy */
   gtk_container_add( GTK_CONTAINER( top_widget ), vbox );
   gtk_box_pack_start( GTK_BOX(vbox), menu_bar, FALSE, FALSE, NO_PADDING);
   gtk_box_pack_start( GTK_BOX(vbox), plot_area, FALSE, FALSE, NO_PADDING );

   /* Pack the Menu Bar and move Help to Far Right */
   gtk_menu_bar_append( GTK_MENU_BAR( menu_bar ), file_item );
   gtk_menu_bar_append( GTK_MENU_BAR( menu_bar ), plot_item );
   gtk_menu_bar_append( GTK_MENU_BAR( menu_bar ), help_item );
   gtk_menu_bar_append( GTK_MENU_BAR( menu_bar ), array1D );
   gtk_menu_item_right_justify( GTK_MENU_ITEM( help_item ) );

   /* Attach and pack the menus */
   gtk_menu_item_set_submenu( GTK_MENU_ITEM( file_item ), file_menu );
   gtk_menu_append( GTK_MENU(file_menu), exit_item );

 /* 5. Showing the Widgets */
   gtk_widget_show_all( top_widget );

 /* 6. Processing Loop */
   gtk_main();
   g_print( "Bye!\n");
   return 0;
}

/*******************************************END OF CODE*******************************/

command used and error which I am getting while compilation is ..
[root arvind Desktop]# gcc thread.c -o run `gtk-config --libs --cflags`
/tmp/ccoiba3m.o(.text+0x1f4): In function `print_screen':
: undefined reference to `g_thread_create'
collect2: ld returned 1 exit status


Thanks and Regards..



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]