A principle benefit of multithreading a graphical user interface is increased responsiveness to user requests. One of the more frustrating aspects of both programming and using applications with graphical user interfaces is dealing with operations that take an indeterminate amount of time. Using threads in such an application provides at minimum a more responsive interface and perhaps one that permits more work to occur by allowing the user to enqueue possible multiple long-running requests.


Why use threads with GTK?

The use of GTK in a threaded environment is desirable for a number of reasons:
  1. It enables GTK to remain responsive to GUI user events. Typical GTK applications use a single thread of execution to perform both GUI and non-GUI processing. Callbacks can lock the GUI, if they block or perform long operations. Threads permit it to stay active.
  2. Threads can simplify the design of complex applications: designing an application as a set of cooperating threads helps to clarify program structure. Single threaded applications have to use non-blocking primitives if they want to overlap communications with execution, which leads to a dramatic increase in program complexity. Multithreading allows overlapping of communication and execution while using high-level (blocking) services.
  3. Threads provide a gateway to multiple processor performance. Parallel programs do not required IPC. Fine-grained parallel tasks can be implemented with threads, where they are impractical with other mechanisms.
  4. Multithreading optimizes throughput, even on uniprocessors. Multithreaded applications automatically use the most efficient IPC-mechanism available---shared memory. Applications get the performance advantages of asynchronous I/O using sychronous calls.

The Problem

Developing multithreaded GUI applications for X-Windows can be a real challenge. Most X-Windows development toolkits, such as Motif 2.0, Athena, GTK, OpenLook and others are not thread-safe. In addition, the supporting X libraries themselves may not thread-safe. Each of these libraries are examples of event loop programming, where much of the state of the application is maintained in library's data structures. But one of the principle structural advantages of threaded programs is the ability to maintain transitional state information directly within the state of each of the threads. This contrast means that integrating threads with event-driven libraries can be difficult. The bottom line: multiple threads cannot always work simultaneously with X-Windows based libraries and toolkits.


If you have comments or suggestions, email me at sdybiec@humanfactor.com