17.1 Multi-threading


17.1 Multi-threading

For those who need a quick reminder “ a multi-processing operating system is one that can, apparently, execute more than one process simultaneously . Each process in turn can have multiple threads running at the same time. The term 'multitasking' generally means running many tasks concurrently, and can be used for both multi-processing (also known as 'process-based multitasking') and multi-threading (also known as ' thread-based multitasking').

If a computer has only one CPU, we are talking about only one real processing sequence. Hence, any multi-processing on a single-CPU machine is 'apparent' only. Each CPU can only run one statement at a time, but it is switching between the different concurrent processes very quickly to give the appearance of handling multiple processes simultaneously. True multi-processing can happen only if you have more than one CPU with each executing its own statements.

Operating systems implement multitasking in one of two ways “ cooperative or pre-emptive. In the former, each thread is responsible for relinquishing control of the CPU so that other threads will get a chance to execute. Older versions of Windows and MS-DOS are based on cooperative multitasking. On the other hand, in pre-emptive multitasking, the scheduler is responsible for allocating time slices to each thread. When a timeslice is up, the scheduler invokes the process of context switching so that the next thread is ready to run its allocated timeslice . Context switching involves copying out the 'state' of the current thread to the stack, and restoring the state of the next thread back from the stack. The 'state' of a thread ncludes register values and any other information needed by the thread for it to run.

Unlike older operating systems which practice cooperative multitasking, Windows NT/2000/XP works on the premise of pre-emptive multitasking. The Windows scheduler decides how many CPU time slices to give to each thread and process currently running, and performs context switching. Cooperative multitasking has the disadvantage that badly written applications, which refuse to give up their hold on the CPU, can hog the whole processor and eventually hang the whole operating system. .NET was designed to run only on pre-emptive multitasking operating systems.

C# has only one keyword specially reserved for threading purposes “ lock (which is equivalent to Java's synchronize keyword). All other threading operations are done via classes in the BCL. In particular, the class which represents a threading object is the System.Threading.Thread class. This is C#'s counterpart of Java's java.lang. Thread .

Both java.lang.Thread and System.Threading.Thread have a number of similar methods for threading operations (such as start, sleep, join, wait, etc.). These methods are largely similar in functionality, and differ only in subtle ways.

This chapter introduces the significant methods of the System.Thread class, and you will have to refer to the API documentation for a full description of the other class members . The more advanced threading classes (such as ThreadPool , Interlocked , and Timer ) will not be covered.

For basic threading operations in C#, three classes of the System.Threading namespace will be useful “ Thread , Monitor , and Mutex . See Figure 17.1.

Figure 17.1. Important classes used for threading operations. Shaded classes are in the System.Threading namespace “ unshaded classes are in the System namespace.

graphics/17fig01.gif

Table 17.1. Some classes, enums, and delegates involved in threading operations and their descriptions

Classes/enums/delegates

Description

Classes

Thread

Represents a threading entity “ contains methods for creating, starting, aborting, suspending, resuming, and other operations on the thread it encapsulates

Mutex

A synchronization primitive used for inter-process synchronization

Monitor

Provides a mechanism for synchronizing access to objects

Exception Classes

ThreadAbortException

Thrown by the runtime when a thread is being aborted (i.e. when a call is made to the Abort() method of the thread)

ThreadInterruptedException

Thrown when a thread is interrupted while in waiting (WaitSleepJoin) state

ThreadStateException

Thrown when a thread operation is requested , but the thread is not in a suitable state for that operation “ For example, when the Start() method of an aborted (dead) thread is invoked, or when the Resume() method of a non- suspended , unstarted, or aborted thread is invoked

Enums

ThreadPriority

Used to specify the scheduling priority of a thread “ often used as a parameter to the Thread.Priority public property

ThreadState

Used to specify the execution states of a thread “ often used as a parameter for the Thread.ThreadState public property

Delegates

ThreadStart

Encapsulates the starting point method when a thread is started

Table 17.1 gives a description of classes, enums, and delegates involved in threading operations.



From Java to C#. A Developers Guide
From Java to C#: A Developers Guide
ISBN: 0321136225
EAN: 2147483647
Year: 2003
Pages: 221
Authors: Heng Ngee Mok

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net