MULTI-THREADING
Multi-threaded NewsWatcher

Contents

THREADING


Multi-threading

Multi-threading gives a program the ability to perform several tasks concurrently. This allows the program to perform lengthy operations in separate threads, enabling the user to continue to interact with the program while these operations take place.

MT-NewsWatcher uses Apple's Thread Manager to perform multiple tasks simultaneously, so that you can get article headers in one window while reading messages from another group at the same time.

MT-NewsWatcher
gets new article counts for three
groups simultaneously.
On the right of the
screen is the Filters
Window, and in the
background a list of
articles for a group.
Click for full size
image (640x480).

What operations
are threaded?

New threads are spawned for the following operations:
  • Checking for new articles
  • Fetching articles (including saving them to files)
  • Fetching group headers
  • Searching
  • Opening article references
  • Extracting binaries
  • Getting news server information
All these operations add a task to the "Tasks" window:

To cancel a task, you can click to select it, then click on the "Cancel" button.

When articles are being fetched, this is also shown at the bottom of the relevant subject window:

In this case, you can cancel the operation by typing an Command-. when either the subject window, or an article window belonging to this subject window is foremost. If more than one article is being fetched, they are cancelled one at a time. All pending fetches are cancelled if you close the subject window.

Extracting binaries

Extracting binaries in multiple threads is one of MT-NewsWatcher's most useful features. The easiest way to do this is to select the articles you wish to extract in a subject window (using the command key for discontiguous selection if necessary), they using the Command-B keyboard shortcut to extract the binaries. This spawns a single thread, in which the selected items are sequentially extracted:

The articles to be extracted are marked with a diamond character, which changes to the check mark when extraction is complete. The progress bar shows the status of the complete download operation. If the operation is cancelled, those articles which were marked with a diamond are marked again as unread.

Having started one extraction thread like this, you can deselect the previous set of articles, select another bunch and do Command-B again to spawn another thread.

Thread and connection limits

MT-NewsWatcher imposes an upper limit of 20 threads, and 6 network connections. To understand how thread and connections interact, you should read this section.

Thread limits

The 20-thread limit determines the number of threads that are pre-allocated when MT-NewsWatcher starts up; this translates into an upper limit of 20 concurrent operations (each of which normally shows a status window). If you already have 20 operations going and attempt to start another one (by opening another group, for example) you will get an error message saying that no free threads are available. This limit of 20 threads can be adjusted by editing the 'ThrD' resource with ResEdit, though increase it will increase the amount of memory allocated in the thread pool.

Connection limits

You can control the maximum number of connections to the news server that MT-NewsWatcher will open in the 'Server Options' panel of the Preferences dialog. This cannot be increased above 6. Normally, you set this at 4, to avoid placing undue load on your news server. If your server imposes an upper limit on the number of connections, then set this value to that upper limit, which ensures that MT-Newswatcher will never attempt to open more than this number of connections.

How connections and threads interact

Every time you perform an operation that spawns a new thread, the program checks to see if you are below the limit on the number of connections. If so, a new connection is opened (or an idle one is reused), and the thread proceeds. If you already have the maximum number of connections active, the thread simply waits until there is a connection spare before it proceeds.

Thus, if you open 20 groups at the same time, 20 threads are spawned, of which the first 4 open news server connections and start to fetch headers. The rest simply wait in the 'Waiting for a free connection' state. When one of the active threads completes, its news server connection is then reused by a waiting thread which can now start to fetch headers from the server.

How to use threads
most efficiently

Having the ability to open multiple threads does not automatically mean that everything will become instantaneous; indeed, improper use of threads may make some operations slower, particularly if you are on a slow network connection. Selecting all your subscribed groups, and hitting 'Return' to open them all up is not a good idea. Things work most efficiently when you have one or two threads which are downloading data, while perhaps reading articles in an open subject window. More threads than this may impact on the responsiveness of the program and, if you are on a modem connection, won't speed things up any. The current threading implementation in MT-NewsWatcher makes no attempt to queue up user commands, to be executed as the resources become available; threads are spawned when the user requests them. This may change in future implementations.

Threading does incur some overhead, in terms of memory in particular, but these are far outweighed by the benefits of not spending hours staring at a rotating cursor while your newsreader gets article headers from the server. The memory overhead is not excessive (somewhere in the region of 24 Kb per thread).


MT-NewsWatcher


Download Basics Speech Recognition Filtering Cool Features