0

I wrote a C/S application using udp and it keeps giving me errors, which I believe has something to do with the way I use threads.

When the client program starts, it first initializes a login window and starts a new thread to listen to the response from the server. After it submits user name and password, the new thread will receive a message indicating whether it submitted the right info. If it did, then the thread would initializes the main GUI window. But it would give strange errors:

Fatal IO error 11 (Resource temporarily unavailable) on X server :0.0

or

python: Fatal IO error 0 (Success) on X server :0.0

I found a similar question here, but it's not solved.

Some say GUI should only be manipulated in the main thread, but others say it's not true.

I also tried using gdk_threads_enter() and gdk_threads_enter() around gtk_main() and the code where I initialize window in that listen thread. But it didn't seem to work.

I don't know much about threads so be patient when pointing out where I have done wrong.

Thanks.

Community
  • 1
  • 1
Gnijuohz
  • 3,294
  • 6
  • 32
  • 47

2 Answers2

1

These error messages, I have found, pop up from time to time when you are not holding the GTK lock properly.

You should put gdk_threads_enter() and gdk_threads_leave() around the original gtk_main() call, and also around every call to a GTK function that takes place

  • outside the thread from which you called gtk_main()
  • but not in a signal, idle, or timeout handler.

This usage is on its way out though as I understand, and in future versions of GTK it will only be possible to manipulate GTK from the main thread.

ptomato
  • 56,175
  • 13
  • 112
  • 165
  • I figured this out yesterday but I have another problem: can't figure out how to change to font style in textview. I do find fontchooser etc but don't know how to apply... – Gnijuohz Jul 16 '12 at 16:24
0

It is true that GTK windows should only be manipulated from the main thread.

That said, in some architectures (notably GNU/Linux) you can manipulate GTK windows from another thread provided that you properly use the global lock with gdk_threads_enter() / gdk_threads_leave(). The key word is "properly", that's not as easy as it seems.

And that said, in some architectures (notably MS-Windows) doing that may seem to work in some simple programs, but will fail miserably in more complex ones.

About your question, you don't say it, but you seem to be using Python somewhere, but you don't say where... Mixing Python and native threads is probably not such a good idea, either.

rodrigo
  • 94,151
  • 12
  • 143
  • 190