Threading

What is Thread ?
Threads are the basic unit to which an operating system allocates processor time, and more than one thread can be executing code inside that process. Each thread maintains exception handlers, a scheduling priority, and a set of structures the system uses to save the thread context until it is scheduled. The thread context includes all the information the thread needs to seamlessly resume execution, including the thread's set of CPU registers and stack, in the address space of the thread's host process.
When to use multiple threads?
To increase responsiveness to the user and decrease the data processing time of your application. If you are doing intensive input/output work, you can also use I/O completion ports to increase your application's responsiveness.
In what kind of scenarios a single application domain could use multiple threads?
Without modification, the same application would dramatically increase user satisfaction when run on a computer with more than one processor. Your single application domain could use multiple threads to accomplish the following tasks:
Communicate over a network, to a Web server, and to a database.
· Perform operations that take a large amount of time.
· Distinguish tasks of varying priority. For example, a high-priority thread manages time-critical tasks, and a low-priority thread performs other tasks.
· Allow the user interface to remain responsive, while allocating time to background tasks.

What is the use of volatile keyword?
The volatile keyword indicates that a field might be modified by multiple threads that are executing at the same time. Fields that are declared volatile are not subject to compiler optimizations that assume access by a single thread. This ensures that the most up-to-date value is present in the field at all times.
The volatile modifier is usually used for a field that is accessed by multiple threads without using the lock statement to serialize access.

What kind of types of fields volatile support?
The volatile keyword can be applied to fields of these types:
· Reference types.
· Pointer types (in an unsafe context). Note that although the pointer itself can be volatile, the object that it points to cannot. In other words, you cannot declare a "pointer to volatile."
· Integral types such as sbyte, byte, short, ushort, int, uint, char, float, and bool.
· An enum type with an integral base type.
· Generic type parameters known to be reference types.
· IntPtr and UIntPtr.

Can I declare a local variable as volatile?
Local variables cannot be declared volatile.