The Advantages of Multithreaded Applications

With the recent proliferation of powerful operating systems like Microsoft Windows NT, combined with the growing popularity of new multiprocessor computers, terms like multithreading and multitasking have become much more common. Unfortunately, a lot of confusion still exists about what these technologies offer and the advantages you can expect as you develop applications.

Multithreading and Multitasking

The terms multitasking, multithreading, and multiprocessing are different technologies often used interchangeably. Multitasking is the ability of an operating system to switch among tasks quickly to give the appearance of simultaneous execution of those tasks.

In a single-threaded environment like Windows 3.1, a task is an application such as Microsoft Word, Excel, or LabVIEW. These applications take turns running on the computer processor, with each application using a small time slice of the processor, then yielding to the next application (Figure 1).

Windows NT and Windows 95 improve on multitasking by transferring the control of processor-time switching to the operating system rather than relying on applications to voluntarily yield. With multitasking working properly, all executing applications on the system appear to progress in parallel.

In many cases, however, the applications can be divided into multiple tasks that also can execute in parallel. Multithreading extends the idea of multitasking to apply within applications, giving applications the capability to separate their own tasks into individual threads (Figure 2).

Then the operating system can divide processing time on these threads similar to the way it divides processing time among entire applications in the exclusively multitasking system. Consequently, the multiple tasks of a multithreaded application also can progress in parallel within the environment of multithreaded operating systems.

While multithreading offers many benefits on single-processor machines, multithreaded applications are essential for taking full advantage of multiprocessor computers. Multiprocessing refers to multiple processors in one computer, executing the threads that are ready to run.

In a symmetric multiprocessing system, the operating system automatically uses all of the processors in the computer to run any threads. Multithreaded programs with parallel executing threads can take full advantage of any number of processors within the system. With multiprocessing power, your multithreaded application can run multiple threads simultaneously, finishing more tasks in less time.

While multiprocessor computers usually are brought into a project to increase performance, single-threaded applications may experience little performance improvement on a new multiprocessor machine. Single-threaded applications use only one processor at any time. In fact, single-threaded applications may adversely affect performance through the overhead of the operating system, switching the application from one processor to another. To achieve maximum performance from multithreaded operating systems and multiprocessor machines, an application must be multithreaded.

Advantages of Multithreading

Windows NT, Windows 95, and some UNIX operating systems are multithreaded and run on either a single-processor machine or a multiprocessor computer. The multithreading software and multiprocessor computers are readily available today, but what does that really offer?

Multithreading brings numerous benefits, especially in PC-based data acquisition (DAQ) and instrument I/O applications. On single-processor machines, multithreading can help applications where some tasks take much longer to execute than others, some tasks need a better deterministic outcome than others, and user interface activity may run concurrently with hardware communications. On multiprocessor systems, multithreading an application takes advantage of the additional hardware and can result in greatly improved overall performance.

Advantages in DAQ and Instrument I/O

Preventing Blocking

PC-based instrumentation applications often communicate with plug-in DAQ boards or stand-alone instruments using standard interfaces including GPIB, VXI, and serial cards to acquire data for test, measurement, and control. Sometimes, these applications encounter synchronous acquisition calls to an instrument or card that take some time to finish executing before returning to the program.

In a single-threaded application, that call effectively prevents the application from performing any other internal tasks that can be done until the acquisition completes. During that synchronous acquisition call where the single-threaded application no longer can use the processor, the operating system switches to a thread from another application on the computer that is ready to run and can use the processor.

On the other hand, a multithreaded application can have pieces of itself on different threads ready to run during the synchronous call. Then, the operating system simply switches the processor to another part of the multithreaded program, allowing it to progress while waiting for the acquisition to complete.

Increasing Responsiveness

Developers also can prioritize threads to increase responsiveness for time- critical threads. Normally, high-priority threads execute for short periods of time and occur only on occasion throughout the program. However, when high-priority threads do occur, they must have priority for using the processor.

A common example of this would be a thread waiting on a digital event to execute a shutdown. Setting priorities can help ensure that the task completes in a timely manner.

Reducing Interference Between Execution and User Interface

High-speed plug-in DAQ applications often are limited, not by acquisition speed but by the rate at which the computer can analyze, display, and log data to file without overflowing buffers or otherwise compromising the integrity of the acquired data. Multithreaded applications can separate the user-interface display on a different thread from the execution system so each thread can run independently at the highest possible speed. The graph may not display every buffer, but it shows as many as it can without interfering with the acquisition.

In addition, developers usually try to optimize their code for the best possible performance combined with display for the system they have. As soon as the system changes in either the DAQ hardware or acquisition rate, they have to edit the code again. With a multithreaded program, however, the individual threads adjust automatically with the available resources if it is programmed correctly.

Maximizing Multiprocessor Performance

Finally, multithreaded applications can take full advantage of multiple processors to gain better performance through simultaneous execution of tasks. A well-implemented multithreaded application efficiently uses all the processors available for its own tasks where a single-threaded application must wait for each task to complete before continuing with the rest of the application. At no time can a single-threaded application execute on more than one processor in the system.

Multithreading applications can help the operating system distribute the processor time more evenly among the different tasks it needs to complete. As a result, all tasks move quickly toward resolution. With the capability to multithread, an application efficiently maximizes the use of the operating system and the computer processors to achieve better responsiveness, minimize blocking problems, or gain better performance with multiprocessor computers.

Implementing Multithreading

Creating multithreaded applications can be difficult, even for very experienced programmers. Multithreading involves a new way of programming the parallel execution of tasks and how they interact with each other vs the traditional line-by-line execution of code. Although many control-flow, text-based languages like C offer libraries for coding multithreaded applications, the very syntax of the language is sequential and presents difficulties to developers trying to visualize the parallel execution of code.

As programmers add more threads to their applications, the complexity of managing those threads jumps exponentially. The situation is analogous to a single developer working on a project vs a team of developers working on the same project. The team can accomplish more in less time, but the members must communicate among themselves to avoid duplication of effort and prevent any accidental collisions in their work.

For example, some unexpected behavior in multithreading can occur when multiple threads request shared resources simultaneously, when priorities prevent threads from gaining appropriate execution time, or when threads sharing data space do not include mutual exclusion code to preserve data integrity. Current tools such as multithread-aware debuggers can help, but ultimately the developer must carefully track the source code to prevent the various conflicts that can happen with parallel executing threads.

Developers using graphical programming languages may find it easier to visualize parallel thread execution because the data-flow syntax allows multiple sections of code to appear in parallel on a single diagram. However, the same problems with communications among threads arise unless the development environment manages the threads for you.

For example, in LabVIEW 5.0, multithreading is built into the environment, including managing the threads. Although no thread programming is required, you still retain the flexibility to set priorities and choose custom configurations for DAQ and instrument control to maximize the benefits of using multithreading.

Regardless of the development environment, multithreading presents many important advantages to developers of instrument control, DAQ, and other applications. Developers creating multithreaded applications can use their computer-based systems to the fullest potential as companies continue to standardize on multithreaded operating systems or acquire increasingly affordable multiprocessor computers to boost performance.

As computer technology constantly evolves at a rapid pace, we need to invest in technology that not only brings immediate benefits, but also provides for the future. Developers who take advantage of multithreading not only can improve the systems they have today, but also can build the foundation to migrate to the multiprocessor systems of tomorrow.

About the Author

Tamra Kerns is the LabVIEW marketing manager at National Instruments. In 1992, Ms. Kerns started with the company as an applications engineer after receiving a B.S. degree in computer engineering from Texas A&M University. National Instruments,

6504 Bridge Point Parkway, Austin, TX 78730, (512) 794-5337.

Copyright 1998 Nelson Publishing Inc.

February 1998

Sponsored Recommendations

Comments

To join the conversation, and become an exclusive member of Electronic Design, create an account today!