|
|
原贴地址:http://zone.ni.com/devzone/cda/tut/p/id/3663
感觉对学习多线程比较有帮助,所以转贴过来,版权归NI公司所有!
Multicore Programming Fundamentals White Paper Series
--------------------------------------------------------------------------------
While they are often used interchangeably, the terms multitasking, multithreading, and multiprocessing all refer to distinctly different concepts. Multitasking refers to the ability of an OS to switch between tasks quickly to give the appearance of simultaneous execution of those tasks. When running in a preemptive multitasking system, applications can be suspended at any time. With multithreading, applications can separate their own tasks into individual threads. In a multithreaded program, the OS directs each thread to execute code for a period of time, referred to as a time slice, before switching execution to another thread. The act of stopping execution of one thread and starting execution of another is referred to as a thread switch. The OS typically can perform thread switches quickly enough to give the appearance of concurrent execution of more than one thread at a time. Multiprocessing refers to using multiple processors on one computer. In a symmetric multiprocessing (SMP) system, the OS automatically uses all of the processors in the computer to run any threads that are ready to run. With multiprocessing power, your multithreaded application can run multiple threads simultaneously, finishing more tasks in less time.
Single-threaded applications may experience little performance improvement by switching to a multiprocessor machine. They execute on either one processor or another but never on all processors at once as multithreaded applications do. Single-threaded applications can experience an adverse affect on performance through the overhead of the OS switching the application from one processor to another. To achieve maximum performance from multithreaded OSs and/or multiprocessor machines, an application must be multithreaded.
目录
Reasons for Multithreading
Choosing Your OS
Introduction to Multithreading in LabWindows/CVI
Running Code in Secondary Threads in LabWindows/CVI
Protecting Data
Avoiding Deadlocks
Monitoring and Controlling Secondary Threads
Process and Thread Priorities
Message Processing
Using Thread-Local Variables
Storing Dynamically Allocated Data in Thread-Local Variables
Callbacks Executing in Separate Threads
Setting the Preferred Processor for a Thread
Additional Multithreading Resources
Reasons for Multithreading
There are four major reasons that you might want to use more than one thread in your program. The most common reason 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.
The second reason you might want to make your program multithreaded is to 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.
The third reason you might want to make your program multithreaded is to 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.
The fourth reason you might want to make your program multithreaded is to perform a particular task in more than one context at the same time. 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 separate 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.
Choosing Your OS
The Microsoft Windows 9x OSs do not work with multiprocessor machines. Therefore, you must run Windows Vista/XP/2000/NT 4.0 on multiprocessor machines to enjoy the benefits of multiple processors. However, even on single-processor machines, multithreaded programs perform better when run on Windows Vista/XP/2000/NT 4.0 than when run on Windows 9x. This is true because of more efficient thread switching in Windows Vista/XP/2000/NT 4.0. However, this difference in performance is generally not noticeable in most multithreaded programs.
Windows Vista/XP/2000/NT 4.0 OSs are more stable than Windows 9x OSs for program development, especially when writing and debugging multithreaded applications. Any time you suspend or terminate a thread that is executing OS code, there is a chance that you will leave some portion of the OS in a bad state. It is far more common for such a condition to crash a machine running a Windows 9x OS than it is for such a condition to crash a machine running Windows Vista/XP/2000/NT 4.0. For this reason, National Instruments recommends that you use a machine running Windows Vista/XP/2000/NT 4.0 for developing multithreaded applications.
[ 本帖最后由 cpubbs 于 2008-6-26 17:35 编辑 ] |
|