Thread created by g_thread _create() cannot write to memory

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

 



I've made a simple program (any variable name mistakes not present in
original program ;]) :
---CODE main.cpp---
#include <gtk/gtk.h>
#include <gdk/gdk.h>
#include <glib.h>
#include <stdio.h>
#include <time.h>
#include <unistd.h>

class SomeClass {
 private:
   GThread *timer_thread;
 public:
   SomeClass();
   static void* update_timer(void* Session);
   int start_timer(void);
   int stop_timer(void);

   //variables
   GtkWindow *MainWindow;
   GtkWidget *timer_widget;
};

int main(int argc, char** argv){
   gtk_init(&argc, &argv);
   g_thread_init(NULL);

   GtkWidget *Window = gtk_window_new( GTK_WINDOW_TOPLEVEL );
   gtk_widget_set_size_request( Window, 400, 400);
   gtk_window_set_title( GTK_WINDOW(Window), "Window");
   gtk_window_set_default_size( GTK_WINDOW(Window), 400, 400);
   gtk_widget_show(Window);

   SomeClass Instance();

   gtk_label_set_text(GTK_LABEL(Instance.timer_widget), "aaa"); //WORKS
NICELY

   gtk_main();

   return 0;
}

SomeClass::SomeClass(){
   this->timer_widget = gtk_label_new("clock");
   this->MainWindow = GTK_WINDOW(window);
   gtk_container_add( GTK_CONTAINER(this->MainWindow), this->timer_widget);
   gtk_widget_show(this->timer_widget);

   this->start_timer();
}

int SomeClass::start_timer(void){
   this->timer_thread = g_thread_create(&SomeClass::update_timer,
(void*) this, FALSE, NULL);
}

int SomeClass::stop_timer(void){
   g_thread_exit(this->timer_thread);
}

void* SomeClass::update_timer(void* Session){
   struct tm* now_tm;
   SomeClass* ThisInst = (SomeClass*) Session;
   char ctime[9];
   time_t tmp_t;
   g_print("%s\n",
                  gtk_label_get_text(GTK_LABEL(ThisInst->timer_widget))
          ); //WORKS NICELY

   gtk_label_set_text( GTK_LABEL(ThisInst->timer_widget), "aaa");
//DOESN"T WORK! SEGFAULTS!
   return (void*) NULL;
}
---END CODE---
Command used to compile:
g++ `pkg-config gtk+-2.0 --libs --cflags` `pkg-config gthread-2.0
--cflags --libs` -fPIC main.cpp -o start

As stated above:
I can set label text from main().
I can get label text from within thread
I CANNOT set label text from within thread.
And my question is why? Or what should I do to be able to write memory?

_______________________________________________

gtk-list@xxxxxxxxx
http://mail.gnome.org/mailman/listinfo/gtk-list

[Index of Archives]     [Touch Screen Library]     [GIMP Users]     [Gnome]     [KDE]     [Yosemite News]     [Steve's Art]

  Powered by Linux