Eric Mumpower
February 28th, 1995
6.033 Report

Are Threads Useful?


Often, the functionality gained by implementing a program using threads does not outweigh the gain in simplicity of creation and ease of debugging gained by serial (threadless) programming. Client programs (e.g. clients like xsetroot, kinit, and bos) which are inherently serial are often best left short, simple, and unthreaded. On the other hand, most server-level programs and many of the more complex user-level programs are well-suited to threading. Complex user-level programs with GUIs (e.g. WYSIWYG word processors, X-based news/mail-readers) are often much more tolerable when the display is updated by a process which need not wait on some time-intensive task to finish, and it is not too difficult to separate a display thread from the algorithmically intensive thread(s).

A specific example of a program which would benefit greatly from threading is a HTTP server. The web server www.mit.edu currently receives roughly 130,000 requests for documents daily. Currently, the machine spawns a new httpd process, with its own address space, for each request. At times, this leads to the machine becoming overloaded by too many outstanding copies of httpd, which causes the machine to fill all available swap space, and eventually fall to its knees, unable to handle the continuing stream of requests from the outside world. Such an overload can be caused by a cluster of spikes in the frequency of client connections.

If we were able to run (1) a simply threaded form of a httpd server, which spawned a new thread for each request, or, better yet, (2) a server process which fed an incoming-request queue and a variable pool of server threads, consuming and complying with queued requests, the danger of such overloading could be greatly reduced. In the first situation, there would be a vast advantage to using a single common address space for all httpd server threads -- the extra memory needed for each copy of the server thread would be minimal compared to the size of the address space needed for new processes under the current setup. In the second situation, the server threads would be able to reduce load spikes by turning the load spike into a slightly longer queue of requests, which could be handled as possible.

However, the extra benefit of a threaded http server is only to be reaped by an extremely high-load server; most web servers have no need for such this degree of complexity in their handling of incoming requests, and suffice merely to spawn a new httpd process for each request. For example, most departmental web servers handle a relatively low number of requests daily, and would see little benefit from a threaded server.

Thus, some complex programs benefit from a threaded implementation, but, in deciding whether to use threads for a particular program, the circumstances of usage weigh as much in the decision as the purpose of the program itself.


Footnotes

xsetroot
an X client which sets the image displayed on the root window
kinit
a Kerberos client which allows one to establish Kerberos authentication
bos
an AFS client which allows for communication with the AFS Basic Overseer Server
httpd
it is not uncommon for 200-300 httpd processes to be running at once, when this happens