Reasons for Multithreading
- Updated2023-02-21
- 3 minute(s) read
Reasons for Multithreading
There are several major reasons that you might want to use more than one thread in your program.
- Separate multiple tasks
- Perform slow input/output operations simultaneously
- Improve performance on a multiprocessor machine
- Perform a particular task in more than one context at a time
Separate Multiple Tasks
The most common reason for multithreading is to separate multiple tasks, one or more of which is time-critical and might be subject to interference by the execution of the other tasks. For example, a program that performs data acquisition and displays a user interface is a good candidate for multithreading. In this type of program, the data acquisition is the time-critical task that might be subject to interference by the user interface task. While using a single-threaded approach in a LabWindows/CVI program, you might decide to pull data from the data acquisition buffer, plot the data to a user interface graph, and then process events to allow the user interface to update. If the user chooses to operate your user interface, for example, by dragging a cursor on a graph, the thread continues to process the user interface events and does not return to the data acquisition task before the data acquisition buffer overflows. Using a multithreaded approach in a LabWindows/CVI program, you might put the data acquisition operations in one thread and display the user interface in another thread. This way, while the user is operating the user interface, the OS performs thread switches to give the data acquisition thread time to perform its task.
Perform Slow Input/Output Operations Simultaneously
For example, a program that uses an instrument to test a circuit board might benefit significantly from multithreading. Using a single-threaded approach in a LabWindows/CVI program, you might send data to the serial port to instruct the circuit board to initialize itself. You wait for the board to complete its operation before initializing the test instrument. You must wait for the test instrument to initialize before taking the measurement. With a multithreaded approach in a LabWindows/CVI program, you might use another thread to initialize the test instrument. This way, you wait for the instrument to initialize while you are waiting for the circuit board to initialize. The slow input/output operations are done simultaneously, thereby reducing the total time you spend waiting.
Improve Performance on a Multiprocessor Machine
Each processor on a machine can execute a thread. So while the OS gives the appearance of concurrent execution of multiple threads on a single-processor machine, the OS actually does execute multiple threads concurrently on a multiprocessor machine. A program that would benefit from multithreading on a multiprocessor machine is one that performs more than one task simultaneously. For example, a program that acquires data, streams it to disk, analyzes the data, and displays the analyzed data in a user interface would likely benefit from being multithreaded and running on a multiprocessor machine. Writing the data to disk and analyzing the data for display are tasks that you can perform simultaneously.
Perform a Task in Multiple Contexts
For example, you might use multithreading in an application that runs tests on parallel test bays. Using a single-threaded approach, the application has to dynamically allocate space for records to hold the results of the test in each bay. The program has to maintain the association between each record and its test bay manually. Using a multithreaded approach, the application can create a secondary thread to handle each test bay. The application can then use thread local variables to create the results records on a per-thread basis. The association between the test bay and its results record is maintained automatically, thereby simplifying the application code.