GTK threads and socket operations

2014-11-26 Thread Sergei Naumov

Hi!

I am writing an application that periodically reads data from network sockets
(from Modbus enabled devices actually) and displays them in GTK+ application. I
read some tutorials about GTK threads and created a separate thread that
encompasses Modbus reads and updates to GTK labels:

modbus_thread = g_thread_new (update_from_modbus,update_from_modbus,NULL);

The update_from_modbus() has a bunch of socket reads (inside 
xantrex_read_value())
and gtk_label_set_text()

sprintf(params_val_text,%-5.1f (%4.1f Hz)\n%-4.1f\n%-4d,
0.001*xantrex_read_value(modbus_pointer,0x004C,RECONSTRUCT_YES),
0.01*xantrex_read_value(modbus_pointer,0x004E,RECONSTRUCT_YES),
0.001*xantrex_read_value(modbus_pointer,0x0054,RECONSTRUCT_YES),
xantrex_read_value(modbus_pointer,0x0052,RECONSTRUCT_YES));
gtk_label_set_text (GTK_LABEL(ac1_params_val),params_val_text);
gtk_label_set_justify(GTK_LABEL(ac1_params_val),GTK_JUSTIFY_LEFT);

However after several hours I see crashes when the program tries to reconnect to
my device. I tried to enclose GTK operations into gtk_threads_enter() /
gtk_threads_leave() but it has no effect. Can anyone, please explain what is
wrong here or may be point to a good example of multithreaded GTK code which has
network socket operations in it?

Thanks in advance,

-- Sergei
___
gtk-app-devel-list mailing list
gtk-app-devel-list@gnome.org
https://mail.gnome.org/mailman/listinfo/gtk-app-devel-list


Re: GTK threads and socket operations

2014-11-26 Thread Bernhard Schuster
You modify your label from a thread that is not the main/ui thread.
Gtk+ is not threadsafe (few exceptions). Use g_idle_add/ g_timeout_add/ and
friends (those are threadsafe) to implicitly serialize your ui
modifications into the gtk mainloop.

Do not use GDK_THREADS_ENTER/LEAVE foo unless you are maintaining a legacy
codebase and you know exactly what this means including its implications.

Best

Bernhard
On Nov 26, 2014 9:02 AM, Sergei Naumov vo...@rambler.ru wrote:

 Hi!

 I am writing an application that periodically reads data from network
 sockets
 (from Modbus enabled devices actually) and displays them in GTK+
 application. I
 read some tutorials about GTK threads and created a separate thread that
 encompasses Modbus reads and updates to GTK labels:

 modbus_thread = g_thread_new (update_from_modbus,update_
 from_modbus,NULL);

 The update_from_modbus() has a bunch of socket reads (inside
 xantrex_read_value())
 and gtk_label_set_text()

 sprintf(params_val_text,%-5.1f (%4.1f Hz)\n%-4.1f\n%-4d,
 0.001*xantrex_read_value(modbus_pointer,0x004C,RECONSTRUCT_YES),
 0.01*xantrex_read_value(modbus_pointer,0x004E,RECONSTRUCT_YES),
 0.001*xantrex_read_value(modbus_pointer,0x0054,RECONSTRUCT_YES),
 xantrex_read_value(modbus_pointer,0x0052,RECONSTRUCT_YES));
 gtk_label_set_text (GTK_LABEL(ac1_params_val),params_val_text);
 gtk_label_set_justify(GTK_LABEL(ac1_params_val),GTK_JUSTIFY_LEFT);

 However after several hours I see crashes when the program tries to
 reconnect to
 my device. I tried to enclose GTK operations into gtk_threads_enter() /
 gtk_threads_leave() but it has no effect. Can anyone, please explain what
 is
 wrong here or may be point to a good example of multithreaded GTK code
 which has
 network socket operations in it?

 Thanks in advance,

 -- Sergei
 ___
 gtk-app-devel-list mailing list
 gtk-app-devel-list@gnome.org
 https://mail.gnome.org/mailman/listinfo/gtk-app-devel-list

___
gtk-app-devel-list mailing list
gtk-app-devel-list@gnome.org
https://mail.gnome.org/mailman/listinfo/gtk-app-devel-list


RE: Re: GTK threads and socket operations

2014-11-26 Thread Sergei Naumov

Is there any tutorial on them? I searched the Web and did not find any good
tutorial on GTK threads.

-- Sergei O. Naumov

 26.11.2014, 12:41:16 пользователь Bernhard Schuster
 (schuster.bernh...@gmail.com) написал:


 You modify your label from a thread that is not the main/ui thread.
 Gtk+ is not threadsafe (few exceptions). Use g_idle_add/ g_timeout_add/ and
 friends (those are threadsafe) to implicitly serialize your ui modifications
 into the gtk mainloop.

 Do not use GDK_THREADS_ENTER/LEAVE foo unless you are maintaining a legacy
 codebase and you know exactly what this means including its implications.

 Best

 Bernhard

 On Nov 26, 2014 9:02 AM, Sergei Naumov vo...@rambler.ru wrote:

   Hi!

   I am writing an application that periodically reads data from network
   sockets
   (from Modbus enabled devices actually) and displays them in GTK+
   application. I
   read some tutorials about GTK threads and created a separate thread that
   encompasses Modbus reads and updates to GTK labels:

   modbus_thread = g_thread_new
   (update_from_modbus,update_from_modbus,NULL);

   The update_from_modbus() has a bunch of socket reads (inside
   xantrex_read_value())
   and gtk_label_set_text()

   sprintf(params_val_text,%-5.1f (%4.1f Hz)\n%-4.1f\n%-4d,
   0.001*xantrex_read_value(modbus_pointer,0x004C,RECONSTRUCT_YES),
   0.01*xantrex_read_value(modbus_pointer,0x004E,RECONSTRUCT_YES),
   0.001*xantrex_read_value(modbus_pointer,0x0054,RECONSTRUCT_YES),
   xantrex_read_value(modbus_pointer,0x0052,RECONSTRUCT_YES));
   gtk_label_set_text (GTK_LABEL(ac1_params_val),params_val_text);
   gtk_label_set_justify(GTK_LABEL(ac1_params_val),GTK_JUSTIFY_LEFT);

   However after several hours I see crashes when the program tries to
   reconnect to
   my device. I tried to enclose GTK operations into gtk_threads_enter() /
   gtk_threads_leave() but it has no effect. Can anyone, please explain what
   is
   wrong here or may be point to a good example of multithreaded GTK code
   which has
   network socket operations in it?

   Thanks in advance,

   -- Sergei
   ___
   gtk-app-devel-list mailing list
   gtk-app-devel-list@gnome.org
   https://mail.gnome.org/mailman/listinfo/gtk-app-devel-list
___
gtk-app-devel-list mailing list
gtk-app-devel-list@gnome.org
https://mail.gnome.org/mailman/listinfo/gtk-app-devel-list

Re: Re: GTK threads and socket operations

2014-11-26 Thread Emmanuele Bassi
hi;

On 26 November 2014 at 10:27, Sergei Naumov vo...@rambler.ru wrote:
 Is there any tutorial on them? I searched the Web and did not find any good
 tutorial on GTK threads.

the only tutorial needed is probably something like:

  Q: Can I use GTK API from different threads?
  A: No.

if you have a long-running operation in a thread, you need to schedule
UI updates in the same main context used by GTK — i.e. use
g_timeout_add(), g_idle_add(), or g_main_context_invoke() to schedule
a function call from your thread, and pass the data needed to update
the UI to that function.

ciao,
 Emmanuele.

  26.11.2014, 12:41:16 пользователь Bernhard Schuster
  (schuster.bernh...@gmail.com) написал:



  You modify your label from a thread that is not the main/ui thread.
  Gtk+ is not threadsafe (few exceptions). Use g_idle_add/ g_timeout_add/ and
  friends (those are threadsafe) to implicitly serialize your ui
 modifications
  into the gtk mainloop.

  Do not use GDK_THREADS_ENTER/LEAVE foo unless you are maintaining a legacy
  codebase and you know exactly what this means including its implications.

  Best

  Bernhard

  On Nov 26, 2014 9:02 AM, Sergei Naumov vo...@rambler.ru wrote:

Hi!

I am writing an application that periodically reads data from network
sockets
(from Modbus enabled devices actually) and displays them in GTK+
application. I
read some tutorials about GTK threads and created a separate thread that
encompasses Modbus reads and updates to GTK labels:

modbus_thread = g_thread_new
(update_from_modbus,update_from_modbus,NULL);

The update_from_modbus() has a bunch of socket reads (inside
xantrex_read_value())
and gtk_label_set_text()

sprintf(params_val_text,%-5.1f (%4.1f Hz)\n%-4.1f\n%-4d,
0.001*xantrex_read_value(modbus_pointer,0x004C,RECONSTRUCT_YES),
0.01*xantrex_read_value(modbus_pointer,0x004E,RECONSTRUCT_YES),
0.001*xantrex_read_value(modbus_pointer,0x0054,RECONSTRUCT_YES),
xantrex_read_value(modbus_pointer,0x0052,RECONSTRUCT_YES));
gtk_label_set_text (GTK_LABEL(ac1_params_val),params_val_text);
gtk_label_set_justify(GTK_LABEL(ac1_params_val),GTK_JUSTIFY_LEFT);

However after several hours I see crashes when the program tries to
reconnect to
my device. I tried to enclose GTK operations into gtk_threads_enter() /
gtk_threads_leave() but it has no effect. Can anyone, please explain what
is
wrong here or may be point to a good example of multithreaded GTK code
which has
network socket operations in it?

Thanks in advance,

-- Sergei
___
gtk-app-devel-list mailing list
gtk-app-devel-list@gnome.org
https://mail.gnome.org/mailman/listinfo/gtk-app-devel-list
 ___
 gtk-app-devel-list mailing list
 gtk-app-devel-list@gnome.org
 https://mail.gnome.org/mailman/listinfo/gtk-app-devel-list



-- 
http://www.bassi.io
[@] ebassi [@gmail.com]
___
gtk-app-devel-list mailing list
gtk-app-devel-list@gnome.org
https://mail.gnome.org/mailman/listinfo/gtk-app-devel-list

Re: GTK threads and socket operations

2014-11-26 Thread Mathieu Comandon
Hi!

Speaking of threading in Gtk, I'm using it quite extensively in my
application and never was really sure I was doing the right thing.

Here are the part in my codebase where I handle background jobs and
downloads:
https://github.com/lutris/lutris/blob/master/lutris/util/jobs.py
https://github.com/lutris/lutris/blob/master/lutris/downloader.py

Most of which is based on:
https://github.com/strycore/pygobject-demos/blob/master/background_task.py

Any feedback from experienced (python) GObject developers would be
greatly appreciated!

Thanks,

Mathieu

On 26/11/2014 11:27, Sergei Naumov wrote:
 Is there any tutorial on them? I searched the Web and did not find any good
 tutorial on GTK threads.
 
 -- Sergei O. Naumov
 
  26.11.2014, 12:41:16 пользователь Bernhard Schuster
  (schuster.bernh...@gmail.com) написал:
 
 
  You modify your label from a thread that is not the main/ui thread.
  Gtk+ is not threadsafe (few exceptions). Use g_idle_add/ g_timeout_add/
 and
  friends (those are threadsafe) to implicitly serialize your ui
 modifications
  into the gtk mainloop.
 
  Do not use GDK_THREADS_ENTER/LEAVE foo unless you are maintaining a legacy
  codebase and you know exactly what this means including its implications.
 
  Best
 
  Bernhard
 
  On Nov 26, 2014 9:02 AM, Sergei Naumov vo...@rambler.ru wrote:
 
Hi!
 
I am writing an application that periodically reads data from network
sockets
(from Modbus enabled devices actually) and displays them in GTK+
application. I
read some tutorials about GTK threads and created a separate thread that
encompasses Modbus reads and updates to GTK labels:
 
modbus_thread = g_thread_new
(update_from_modbus,update_from_modbus,NULL);
 
The update_from_modbus() has a bunch of socket reads (inside
xantrex_read_value())
and gtk_label_set_text()
 
sprintf(params_val_text,%-5.1f (%4.1f Hz)\n%-4.1f\n%-4d,
0.001*xantrex_read_value(modbus_pointer,0x004C,RECONSTRUCT_YES),
0.01*xantrex_read_value(modbus_pointer,0x004E,RECONSTRUCT_YES),
0.001*xantrex_read_value(modbus_pointer,0x0054,RECONSTRUCT_YES),
xantrex_read_value(modbus_pointer,0x0052,RECONSTRUCT_YES));
gtk_label_set_text (GTK_LABEL(ac1_params_val),params_val_text);
gtk_label_set_justify(GTK_LABEL(ac1_params_val),GTK_JUSTIFY_LEFT);
 
However after several hours I see crashes when the program tries to
reconnect to
my device. I tried to enclose GTK operations into gtk_threads_enter() /
gtk_threads_leave() but it has no effect. Can anyone, please explain
 what
is
wrong here or may be point to a good example of multithreaded GTK code
which has
network socket operations in it?
 
Thanks in advance,
 
-- Sergei
___
gtk-app-devel-list mailing list
gtk-app-devel-list@gnome.org
https://mail.gnome.org/mailman/listinfo/gtk-app-devel-list
 ___
 gtk-app-devel-list mailing list
 gtk-app-devel-list@gnome.org
 https://mail.gnome.org/mailman/listinfo/gtk-app-devel-list
___
gtk-app-devel-list mailing list
gtk-app-devel-list@gnome.org
https://mail.gnome.org/mailman/listinfo/gtk-app-devel-list

Re: GTK threads

2009-07-17 Thread Marshall Lake


Just to let everyone know ...

I discovered that I was having an issue with my socket logic which was the 
main cause of my problem outlined below.  Once I resolved that issue and 
incorporated g_idle_add() along with GCond  GMutex into my code 
everything is working.




(When you reply, please add CC to devel mailing list, so other peoples 
know it's progress and may help you more).


--- ml...@mlake.net wrote:

From: Marshall Lake ml...@mlake.net
To: Ardhan Madras aj...@knac.com
Subject: Re: GTK  threads
Date: Wed, 8 Jul 2009 12:21:34 -0400 (EDT)


Thanks for your feedback.  I think what you say is likely my problem.



On Wed, 8 Jul 2009, Ardhan Madras wrote:


Hi,

You should be aware when call GTK+ function in another thread, if you 
do this from another thread, GTK+ is likely not send commands to the X 
server. When you run a dialog in the main thread, main loop is blocked, 
so the dialog's window remain displayed until a response was given (eg. 
button was clicked). But in you code, you run the dialog outside the 
main loop (main thread) the main loop of course is not blocked, and it 
fails to show the dialog. See 
http://library.gnome.org/devel/gtk-faq/stable/x499.html on howto send 
GTK+ funcs to the main loop. Another way is to provide mechanism so you 
can run the dialog from the main thread by using such as condition 
variable (GCond).


-- ajhwb

--- ml...@mlake.net wrote:

From: Marshall Lake ml...@mlake.net
To: gtk-app-devel-list@gnome.org
Subject: GTK  threads
Date: Wed, 8 Jul 2009 09:38:30 -0400 (EDT)


I posted this on gtk-list but in the hope of catching a wider audience ...

I'm developing a program(s) which runs in the client/server environment.
I'm using GTK, sockets, and threads.  (GTK and threads only on the client
side.)  I don't have very much experience using threads and this is the
first time I've tried to call GTK functions within a thread, and I'm
having problems.  I've tried different ways of approaching the problem but
can't seem to get anywhere.  I suspect there's something I don't
understand about thread processing or I'm under some misconception.
Maybe someone can help me?  Any help is much appreciated.

I get the following errors when trying to run the code below (the errors
show up when the program calls gtk_dialog_run() in the thread
(Wait4Challenge()):

src/xcb_io.c:242: process_responses: Assertion `(((long) (dpy-last_request_read) - 
(long) (dpy-request)) = 0)' failed.
Fatal IO error 0 (Success) on X server :0.0.


void *
Wait4Challenge () {
  int wsock, x, port = 0, listensock, connected;
  char buffer[5000], *msg[5], challenger[50];

  listensock = -1;
  for (x = 0; x  5; x++)
  msg[x] = NULL;

  wsock = get_connection (SOCK_STREAM, port, listensock);

  connected = 1;
  while (connected) {
  GtkWidget *dialog;
  int response;

  if (sock_gets (wsock, buffer[0], sizeof (buffer))  0) {
  msg[0] = There is a problem with the server.\n\n;
  outMessage (msg);
  connected = 0;
  }

  if (buffer[0] == 'R')
  connected = 0;

  if (buffer[0] != 'C')
  continue;

  strcpy (challenger[0], buffer[1]);

  gdk_threads_enter ();

  dialog = gtk_message_dialog_new (GTK_WINDOW (mainwin), 
GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_QUESTION,
  GTK_BUTTONS_YES_NO, %s has challenged you to a game.  
Do you wish to play?, challenger);
  gtk_window_set_title (GTK_WINDOW (dialog), Play Challenger?);
  response = gtk_dialog_run (GTK_DIALOG (dialog));
  gtk_widget_destroy (dialog);

  gdk_flush();
  gdk_threads_leave ();

  if (response == GTK_RESPONSE_YES) {
  sock_puts (wsock, OK\n);
  connected = 0;
  }
  else {
  sock_puts (wsock, NO\n);
  for (x = 0; x  5; x++)
  msg[x] = NULL;
  continue;
  }
  }

  shutdown (wsock, SHUT_RDWR);
  close (wsock);
  return (NULL);
}

int
main (int argc, char *argv[]) {
  g_thread_init (NULL);
  gdk_threads_init ();

  gtk_init (argc, argv);

  mainwin = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  gtk_widget_set_usize (mainwin, WINSIZEX, WINSIZEY);
  gtk_widget_realize (mainwin);

  gtk_widget_add_events (mainwin, GDK_BUTTON_PRESS_MASK);
  gtk_signal_connect (GTK_OBJECT (mainwin), delete_event, GTK_SIGNAL_FUNC 
(delete_event), 0);

  gtk_widget_show_all (mainwin);

  if (!g_thread_create (Wait4Challenge, NULL, FALSE, NULL))
  syslog (LOG_INFO, error trying to create new thread);

  gdk_threads_enter ();

  gtk_main ();

  gdk_flush();
  gdk_threads_leave ();

  return 0;
}

--
Marshall Lake -- ml...@mlake.net -- http://mlake.net



--
Marshall Lake -- ml...@mlake.net -- http://mlake.net

GTK threads

2009-07-08 Thread Marshall Lake


I posted this on gtk-list but in the hope of catching a wider audience ...

I'm developing a program(s) which runs in the client/server environment. 
I'm using GTK, sockets, and threads.  (GTK and threads only on the client 
side.)  I don't have very much experience using threads and this is the 
first time I've tried to call GTK functions within a thread, and I'm 
having problems.  I've tried different ways of approaching the problem but 
can't seem to get anywhere.  I suspect there's something I don't 
understand about thread processing or I'm under some misconception. 
Maybe someone can help me?  Any help is much appreciated.


I get the following errors when trying to run the code below (the errors 
show up when the program calls gtk_dialog_run() in the thread 
(Wait4Challenge()):


src/xcb_io.c:242: process_responses: Assertion `(((long) (dpy-last_request_read) - 
(long) (dpy-request)) = 0)' failed.
Fatal IO error 0 (Success) on X server :0.0.


void *
Wait4Challenge () {
  int wsock, x, port = 0, listensock, connected;
  char buffer[5000], *msg[5], challenger[50];

  listensock = -1;
  for (x = 0; x  5; x++)
  msg[x] = NULL;

  wsock = get_connection (SOCK_STREAM, port, listensock);

  connected = 1;
  while (connected) {
  GtkWidget *dialog;
  int response;

  if (sock_gets (wsock, buffer[0], sizeof (buffer))  0) {
  msg[0] = There is a problem with the server.\n\n;
  outMessage (msg);
  connected = 0;
  }

  if (buffer[0] == 'R')
  connected = 0;

  if (buffer[0] != 'C')
  continue;

  strcpy (challenger[0], buffer[1]);

  gdk_threads_enter ();

  dialog = gtk_message_dialog_new (GTK_WINDOW (mainwin), 
GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_QUESTION,
  GTK_BUTTONS_YES_NO, %s has challenged you to a game.  
Do you wish to play?, challenger);
  gtk_window_set_title (GTK_WINDOW (dialog), Play Challenger?);
  response = gtk_dialog_run (GTK_DIALOG (dialog));
  gtk_widget_destroy (dialog);

  gdk_flush();
  gdk_threads_leave ();

  if (response == GTK_RESPONSE_YES) {
  sock_puts (wsock, OK\n);
  connected = 0;
  }
  else {
  sock_puts (wsock, NO\n);
  for (x = 0; x  5; x++)
  msg[x] = NULL;
  continue;
  }
  }

  shutdown (wsock, SHUT_RDWR);
  close (wsock);
  return (NULL);
}

int
main (int argc, char *argv[]) {
  g_thread_init (NULL);
  gdk_threads_init ();

  gtk_init (argc, argv);

  mainwin = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  gtk_widget_set_usize (mainwin, WINSIZEX, WINSIZEY);
  gtk_widget_realize (mainwin);

  gtk_widget_add_events (mainwin, GDK_BUTTON_PRESS_MASK);
  gtk_signal_connect (GTK_OBJECT (mainwin), delete_event, GTK_SIGNAL_FUNC 
(delete_event), 0);

  gtk_widget_show_all (mainwin);

  if (!g_thread_create (Wait4Challenge, NULL, FALSE, NULL))
  syslog (LOG_INFO, error trying to create new thread);

  gdk_threads_enter ();

  gtk_main ();

  gdk_flush();
  gdk_threads_leave ();

  return 0;
}

--
Marshall Lake -- ml...@mlake.net -- http://mlake.net
___
gtk-app-devel-list mailing list
gtk-app-devel-list@gnome.org
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list


Re: GTK threads

2009-07-08 Thread Ardhan Madras
Hi,

You should be aware when call GTK+ function in another thread, if you do this 
from another thread, GTK+ is likely not send commands to the X server. When you 
run a dialog in the main thread, main loop is blocked, so the dialog's window 
remain displayed until a response was given (eg. button was clicked). But in 
you code, you run the dialog outside the main loop (main thread) the main loop 
of course is not blocked, and it fails to show the dialog. See 
http://library.gnome.org/devel/gtk-faq/stable/x499.html on howto send GTK+ 
funcs to the main loop. Another way is to provide mechanism so you can run the 
dialog from the main thread by using such as condition variable (GCond).

-- ajhwb

--- ml...@mlake.net wrote:

From: Marshall Lake ml...@mlake.net
To: gtk-app-devel-list@gnome.org
Subject: GTK  threads
Date: Wed, 8 Jul 2009 09:38:30 -0400 (EDT)


I posted this on gtk-list but in the hope of catching a wider audience ...

I'm developing a program(s) which runs in the client/server environment. 
I'm using GTK, sockets, and threads.  (GTK and threads only on the client 
side.)  I don't have very much experience using threads and this is the 
first time I've tried to call GTK functions within a thread, and I'm 
having problems.  I've tried different ways of approaching the problem but 
can't seem to get anywhere.  I suspect there's something I don't 
understand about thread processing or I'm under some misconception. 
Maybe someone can help me?  Any help is much appreciated.

I get the following errors when trying to run the code below (the errors 
show up when the program calls gtk_dialog_run() in the thread 
(Wait4Challenge()):

src/xcb_io.c:242: process_responses: Assertion `(((long) 
(dpy-last_request_read) - (long) (dpy-request)) = 0)' failed.
Fatal IO error 0 (Success) on X server :0.0.


void *
Wait4Challenge () {
   int wsock, x, port = 0, listensock, connected;
   char buffer[5000], *msg[5], challenger[50];

   listensock = -1;
   for (x = 0; x  5; x++)
   msg[x] = NULL;

   wsock = get_connection (SOCK_STREAM, port, listensock);

   connected = 1;
   while (connected) {
   GtkWidget *dialog;
   int response;

   if (sock_gets (wsock, buffer[0], sizeof (buffer))  0) {
   msg[0] = There is a problem with the server.\n\n;
   outMessage (msg);
   connected = 0;
   }

   if (buffer[0] == 'R')
   connected = 0;

   if (buffer[0] != 'C')
   continue;

   strcpy (challenger[0], buffer[1]);

   gdk_threads_enter ();

   dialog = gtk_message_dialog_new (GTK_WINDOW (mainwin), 
GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_QUESTION,
   GTK_BUTTONS_YES_NO, %s has challenged you to a 
game.  Do you wish to play?, challenger);
   gtk_window_set_title (GTK_WINDOW (dialog), Play Challenger?);
   response = gtk_dialog_run (GTK_DIALOG (dialog));
   gtk_widget_destroy (dialog);

   gdk_flush();
   gdk_threads_leave ();

   if (response == GTK_RESPONSE_YES) {
   sock_puts (wsock, OK\n);
   connected = 0;
   }
   else {
   sock_puts (wsock, NO\n);
   for (x = 0; x  5; x++)
   msg[x] = NULL;
   continue;
   }
   }

   shutdown (wsock, SHUT_RDWR);
   close (wsock);
   return (NULL);
}

int
main (int argc, char *argv[]) {
   g_thread_init (NULL);
   gdk_threads_init ();

   gtk_init (argc, argv);

   mainwin = gtk_window_new (GTK_WINDOW_TOPLEVEL);
   gtk_widget_set_usize (mainwin, WINSIZEX, WINSIZEY);
   gtk_widget_realize (mainwin);

   gtk_widget_add_events (mainwin, GDK_BUTTON_PRESS_MASK);
   gtk_signal_connect (GTK_OBJECT (mainwin), delete_event, 
GTK_SIGNAL_FUNC (delete_event), 0);

   gtk_widget_show_all (mainwin);

   if (!g_thread_create (Wait4Challenge, NULL, FALSE, NULL))
   syslog (LOG_INFO, error trying to create new thread);

   gdk_threads_enter ();

   gtk_main ();

   gdk_flush();
   gdk_threads_leave ();

   return 0;
}

-- 
Marshall Lake -- ml...@mlake.net -- http://mlake.net
___
gtk-app-devel-list mailing list
gtk-app-devel-list@gnome.org
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list




_
Listen to KNAC, Hit the Home page and Tune In Live! --- http://www.knac.com
___
gtk-app-devel-list mailing list
gtk-app-devel-list@gnome.org
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list


Re: GTK threads

2009-07-08 Thread Gregory Hosler
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA1

Marshall Lake wrote:
 
 I posted this on gtk-list but in the hope of catching a wider audience ...
 
 I'm developing a program(s) which runs in the client/server environment.
 I'm using GTK, sockets, and threads.  (GTK and threads only on the
 client side.)  I don't have very much experience using threads and this
 is the first time I've tried to call GTK functions within a thread, and
 I'm having problems.  I've tried different ways of approaching the
 problem but can't seem to get anywhere.  I suspect there's something I
 don't understand about thread processing or I'm under some
 misconception. Maybe someone can help me?  Any help is much appreciated.

If I'm not mistaken, X isn't tread-safe, which may be the root cause of the
specific error message that you're seeing. The implication is that any call
which may invoke X should be done from the main thread. When an auxiliary thread
does need to do some drawing, one way to achieve that would be with a
g_idle_add(), which will get invoked by the main thread when there is nothing
else to do.

Hope this helps,

- -Greg

 I get the following errors when trying to run the code below (the errors
 show up when the program calls gtk_dialog_run() in the thread
 (Wait4Challenge()):
 
 src/xcb_io.c:242: process_responses: Assertion `(((long)
 (dpy-last_request_read) - (long) (dpy-request)) = 0)' failed.
 Fatal IO error 0 (Success) on X server :0.0.
 
 
 void *
 Wait4Challenge () {
   int wsock, x, port = 0, listensock, connected;
   char buffer[5000], *msg[5], challenger[50];
 
   listensock = -1;
   for (x = 0; x  5; x++)
   msg[x] = NULL;
 
   wsock = get_connection (SOCK_STREAM, port, listensock);
 
   connected = 1;
   while (connected) {
   GtkWidget *dialog;
   int response;
 
   if (sock_gets (wsock, buffer[0], sizeof (buffer))  0) {
   msg[0] = There is a problem with the server.\n\n;
   outMessage (msg);
   connected = 0;
   }
 
   if (buffer[0] == 'R')
   connected = 0;
 
   if (buffer[0] != 'C')
   continue;
 
   strcpy (challenger[0], buffer[1]);
 
   gdk_threads_enter ();
 
   dialog = gtk_message_dialog_new (GTK_WINDOW (mainwin),
 GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_QUESTION,
   GTK_BUTTONS_YES_NO, %s has challenged you
 to a game.  Do you wish to play?, challenger);
   gtk_window_set_title (GTK_WINDOW (dialog), Play Challenger?);
   response = gtk_dialog_run (GTK_DIALOG (dialog));
   gtk_widget_destroy (dialog);
 
   gdk_flush();
   gdk_threads_leave ();
 
   if (response == GTK_RESPONSE_YES) {
   sock_puts (wsock, OK\n);
   connected = 0;
   }
   else {
   sock_puts (wsock, NO\n);
   for (x = 0; x  5; x++)
   msg[x] = NULL;
   continue;
   }
   }
 
   shutdown (wsock, SHUT_RDWR);
   close (wsock);
   return (NULL);
 }
 
 int
 main (int argc, char *argv[]) {
   g_thread_init (NULL);
   gdk_threads_init ();
 
   gtk_init (argc, argv);
 
   mainwin = gtk_window_new (GTK_WINDOW_TOPLEVEL);
   gtk_widget_set_usize (mainwin, WINSIZEX, WINSIZEY);
   gtk_widget_realize (mainwin);
 
   gtk_widget_add_events (mainwin, GDK_BUTTON_PRESS_MASK);
   gtk_signal_connect (GTK_OBJECT (mainwin), delete_event,
 GTK_SIGNAL_FUNC (delete_event), 0);
 
   gtk_widget_show_all (mainwin);
 
   if (!g_thread_create (Wait4Challenge, NULL, FALSE, NULL))
   syslog (LOG_INFO, error trying to create new thread);
 
   gdk_threads_enter ();
 
   gtk_main ();
 
   gdk_flush();
   gdk_threads_leave ();
 
   return 0;
 }
 


- --
+-+

Please also check the log file at /dev/null for additional information.
(from /var/log/Xorg.setup.log)

| Greg Hosler   ghos...@redhat.com|
+-+
-BEGIN PGP SIGNATURE-
Version: GnuPG v1.4.9 (GNU/Linux)
Comment: Using GnuPG with Fedora - http://enigmail.mozdev.org

iEYEARECAAYFAkpU99MACgkQ404fl/0CV/QP0gCdFjSI6ASkdRfr70QXpy5amjQn
z28AoKG55bgTe2t9dUERYc1G8wxPULaT
=DMvb
-END PGP SIGNATURE-
___
gtk-app-devel-list mailing list
gtk-app-devel-list@gnome.org
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list


Re: GTK threads

2009-07-08 Thread Ardhan Madras
(When you reply, please add CC to devel mailing list, so other peoples know 
it's progress and may help you more).

--- ml...@mlake.net wrote:

From: Marshall Lake ml...@mlake.net
To: Ardhan Madras aj...@knac.com
Subject: Re: GTK  threads
Date: Wed, 8 Jul 2009 12:21:34 -0400 (EDT)


Thanks for your feedback.  I think what you say is likely my problem.



On Wed, 8 Jul 2009, Ardhan Madras wrote:

 Hi,

 You should be aware when call GTK+ function in another thread, if you do this 
 from another thread, GTK+ is likely not send commands to the X server. When 
 you run a dialog in the main thread, main loop is blocked, so the dialog's 
 window remain displayed until a response was given (eg. button was clicked). 
 But in you code, you run the dialog outside the main loop (main thread) the 
 main loop of course is not blocked, and it fails to show the dialog. See 
 http://library.gnome.org/devel/gtk-faq/stable/x499.html on howto send GTK+ 
 funcs to the main loop. Another way is to provide mechanism so you can run 
 the dialog from the main thread by using such as condition variable (GCond).

 -- ajhwb

 --- ml...@mlake.net wrote:

 From: Marshall Lake ml...@mlake.net
 To: gtk-app-devel-list@gnome.org
 Subject: GTK  threads
 Date: Wed, 8 Jul 2009 09:38:30 -0400 (EDT)


 I posted this on gtk-list but in the hope of catching a wider audience ...

 I'm developing a program(s) which runs in the client/server environment.
 I'm using GTK, sockets, and threads.  (GTK and threads only on the client
 side.)  I don't have very much experience using threads and this is the
 first time I've tried to call GTK functions within a thread, and I'm
 having problems.  I've tried different ways of approaching the problem but
 can't seem to get anywhere.  I suspect there's something I don't
 understand about thread processing or I'm under some misconception.
 Maybe someone can help me?  Any help is much appreciated.

 I get the following errors when trying to run the code below (the errors
 show up when the program calls gtk_dialog_run() in the thread
 (Wait4Challenge()):

 src/xcb_io.c:242: process_responses: Assertion `(((long) 
 (dpy-last_request_read) - (long) (dpy-request)) = 0)' failed.
 Fatal IO error 0 (Success) on X server :0.0.


 void *
 Wait4Challenge () {
   int wsock, x, port = 0, listensock, connected;
   char buffer[5000], *msg[5], challenger[50];

   listensock = -1;
   for (x = 0; x  5; x++)
   msg[x] = NULL;

   wsock = get_connection (SOCK_STREAM, port, listensock);

   connected = 1;
   while (connected) {
   GtkWidget *dialog;
   int response;

   if (sock_gets (wsock, buffer[0], sizeof (buffer))  0) {
   msg[0] = There is a problem with the server.\n\n;
   outMessage (msg);
   connected = 0;
   }

   if (buffer[0] == 'R')
   connected = 0;

   if (buffer[0] != 'C')
   continue;

   strcpy (challenger[0], buffer[1]);

   gdk_threads_enter ();

   dialog = gtk_message_dialog_new (GTK_WINDOW (mainwin), 
 GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_QUESTION,
   GTK_BUTTONS_YES_NO, %s has challenged you to a 
 game.  Do you wish to play?, challenger);
   gtk_window_set_title (GTK_WINDOW (dialog), Play Challenger?);
   response = gtk_dialog_run (GTK_DIALOG (dialog));
   gtk_widget_destroy (dialog);

   gdk_flush();
   gdk_threads_leave ();

   if (response == GTK_RESPONSE_YES) {
   sock_puts (wsock, OK\n);
   connected = 0;
   }
   else {
   sock_puts (wsock, NO\n);
   for (x = 0; x  5; x++)
   msg[x] = NULL;
   continue;
   }
   }

   shutdown (wsock, SHUT_RDWR);
   close (wsock);
   return (NULL);
 }

 int
 main (int argc, char *argv[]) {
   g_thread_init (NULL);
   gdk_threads_init ();

   gtk_init (argc, argv);

   mainwin = gtk_window_new (GTK_WINDOW_TOPLEVEL);
   gtk_widget_set_usize (mainwin, WINSIZEX, WINSIZEY);
   gtk_widget_realize (mainwin);

   gtk_widget_add_events (mainwin, GDK_BUTTON_PRESS_MASK);
   gtk_signal_connect (GTK_OBJECT (mainwin), delete_event, 
 GTK_SIGNAL_FUNC (delete_event), 0);

   gtk_widget_show_all (mainwin);

   if (!g_thread_create (Wait4Challenge, NULL, FALSE, NULL))
   syslog (LOG_INFO, error trying to create new thread);

   gdk_threads_enter ();

   gtk_main ();

   gdk_flush();
   gdk_threads_leave ();

   return 0;
 }

 -- 
 Marshall Lake -- ml...@mlake.net -- http://mlake.net
 ___
 gtk-app-devel-list mailing list
 gtk-app-devel-list@gnome.org
 http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list




 _
 Listen to KNAC, Hit the Home page and Tune

Problem with gtk threads...

2007-10-04 Thread Suma H S
hi,
I am creating a threaded Gtk Application where threads
are used for doing some time consuming computations. 
From the threads, I need to update the UI. I have surrounded 
the Gtk code with gdk_threads_enter() and gdk_threads_leave(). 
The code works fine, however I have encountered two instances 
where the application hangs:

1) when I try to create and show a pop_up window from the thread,
in the line gtk_window_show(pop_up_window).

2) when I update the contents of the treeview using list_store_set, in the
line gtk_tree_view_set_model()

Anyone has any idea why this happens?

Thanks in advance,
Suma  H S



The information contained in this electronic message and any attachments to 
this message are intended for the exclusive use of the addressee(s) and may 
contain proprietary, confidential or privileged information. If you are not the 
intended recipient, you should not disseminate, distribute or copy this e-mail. 
Please notify the sender immediately and destroy all copies of this message and 
any attachments contained in it.

Contact your Administrator for further information.
___
gtk-app-devel-list mailing list
gtk-app-devel-list@gnome.org
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list


Re: Problem with gtk threads...

2007-10-04 Thread Tor Lillqvist
 From the threads, I need to update the UI.

The overwhelming consensus, I think, is that even if possible (on X11,
with proper use of the gdk locking functions etc), this is a bad idea.
Instead, call g_timeout_add() in your threads to schedule functions to
be run in the main GUI thread, and have these functions then update
the GUI.

 The information contained in this electronic message and any attachments
 to this message are intended [etc]

You will probably be more flamed for this part than you will get
useful answers to your technical question. Lawyerspeak like this is
utterly stupid to add to messages sent to public mailing lists that
are archived publicly and readable by anybody.

--tml
___
gtk-app-devel-list mailing list
gtk-app-devel-list@gnome.org
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list


Re: Trying to make some workaround for win32 + gtk + threads bug

2006-09-11 Thread Chris Sparks
One approach could be to have a GTK thread that handles all other 
thread's GTK calls, like an
X Server.  Not trivial but you could isolate the choice for commands 
that you'll serve.

Chris

Tor Lillqvist wrote:

Tomasz Jankowski writes:
  Now I want to know, what I can do with widgets created in main
  thread. I noted, that I can't show/hide widgets from various
  threads, are there some more things, which I can't do with widgets
  from varoius threads on Win32?

I don't think any systematic experimentation has been done. We don't
promise you can do anything; if you experiment and it turns out you
can do something, it's on your own risk...

  Was this bug even in small piece solved since gtk+ v2.8?

Well, a few workaround was suggested in bug #60620, but how useful
they are I don't know.

--tml

___
gtk-app-devel-list mailing list
gtk-app-devel-list@gnome.org
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list

  


___
gtk-app-devel-list mailing list
gtk-app-devel-list@gnome.org
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list


Trying to make some workaround for win32 + gtk + threads bug

2006-09-10 Thread Tomasz Jankowski
Hi!

I've already wrote some code, which make me able to create widgets from
diffrent threads and it seems to be working (I used GAsyncQueue for it). Now
I want to know, what I can do with widgets created in main thread. I noted,
that I can't show/hide widgets from various threads, are there some more
things, which I can't do with widgets from varoius threads on Win32?

Was this bug even in small piece solved since gtk+ v2.8?

-- 
Pozdrawiam!
Tom
___
gtk-app-devel-list mailing list
gtk-app-devel-list@gnome.org
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list


Re: Trying to make some workaround for win32 + gtk + threads bug

2006-09-10 Thread Tor Lillqvist
Tomasz Jankowski writes:
  Now I want to know, what I can do with widgets created in main
  thread. I noted, that I can't show/hide widgets from various
  threads, are there some more things, which I can't do with widgets
  from varoius threads on Win32?

I don't think any systematic experimentation has been done. We don't
promise you can do anything; if you experiment and it turns out you
can do something, it's on your own risk...

  Was this bug even in small piece solved since gtk+ v2.8?

Well, a few workaround was suggested in bug #60620, but how useful
they are I don't know.

--tml

___
gtk-app-devel-list mailing list
gtk-app-devel-list@gnome.org
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list