Threading provides the ability to create
the application that usage more than one thread of execution.
Process a process can have a
user interface thread that manages interactions with the user and worker
threads that perform other tasks while the user interface thread waits for user
input.
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
**The .NET Framework further
subdivides an operating system process into lightweight managed subprocesses, called
application domains, represented by System.AppDomain.
Although each application domain is
started with a single thread, code in that application domain can create
additional application domains and additional threads. The result is that a
managed thread can move freely between application domains inside the same
managed process; you might have only one thread moving among several
application domains.
When To Use Multiple Threads
Software that requires
user interaction must react to the user's activities as rapidly as possible to
provide a rich user experience. At the same time, however, it must do the
calculations necessary to present data to the user as fast as possible.
Disadvantages
of Multiple Threads
Lock
Statement
Disadvantages
of Multiple Threads
It is recommended that
you use as few threads as possible, thereby minimizing the use of
operating-system resources and improving performance. Threading also has
resource requirements and potential conflicts to be considered when designing
your application. The resource requirements are as follows:
- The system consumes memory for the context information
required by processes, AppDomain objects,
and threads. Therefore, the number of processes, AppDomain objects,
and threads that can be created is limited by available memory.
- Keeping track of a large number of threads consumes
significant processor time. If there are too many threads, most of them
will not make significant progress. If most of the current threads are in
one process, threads in other processes are scheduled less frequently.
- Controlling code execution with many threads is
complex, and can be a source of many bugs.
- Destroying threads requires knowing what could happen
and handling those issues.
Lock
Statement
The lock keyword marks a statement block as a
critical section by obtaining the mutual-exclusion lock for a given object,
executing a statement, and then releasing the lock.
using System;
using System.Threading;
using System.Diagnostics;
namespace nestedlocking
{
class Program
{
static object x = new object();
using System.Threading;
using System.Diagnostics;
namespace nestedlocking
{
class Program
{
static object x = new object();
static void Main()
{
lock (x)
{
Console.WriteLine("Another method");
}
Console.Read();
}
}
}
{
lock (x)
{
Console.WriteLine("Another method");
}
Console.Read();
}
}
}
MUTEX:
A Mutex is like a C#
lock, but it can work across multiple processes. In other words, Mutex can be
computer-wide as well as application-wide.
When two or more
threads need to access a shared resource at the same time, the system needs a
synchronization mechanism to ensure that only one thread at a time uses the
resource. Mutex is a synchronization primitive that grants exclusive
access to the shared resource to only one thread. If a thread acquires a
Mutex, the second thread that wants to acquire that Mutex is suspended until
the first thread releases the Mutex.
A critical section is a piece of code that accesses
a shared resource (data structure or device) but the condition is that only one thread can
enter in this section at a time.
With a Mutex class, you
call the WaitHandle.WaitOne method
to lock and ReleaseMutex to unlock. Closing or disposing a Mutex automatically
releases it. Just as with the lock statement, a Mutex can be released only from
the same thread that obtained it
Example:
using System;
using System.Collections;
using System.Threading;
namespace Mutexclass
{
class vinod
{
private static Mutex mutex = new Mutex();
private const int numhits = 1;
private const int numThreads = 4;
private static void ThreadProcess()
{
for (int i = 0; i < numhits; i++)
{
UseCsharpcorner();
}
}
private static void UseCsharpcorner()
{
mutex.WaitOne(); // Wait
until it is safe to enter.
Console.WriteLine("{0} has
entered in the vinodsinghjnp.blogspot.com",
Thread.CurrentThread.Name);
// Place
code to access non-reentrant resources here.
Thread.Sleep(500); // Wait until it is safe to enter.
Console.WriteLine("{0} is
leaving the vinodsinghjnp.blogspot.com\r\n",
Thread.CurrentThread.Name);
mutex.ReleaseMutex(); //
Release the Mutex.
}
static void Main(string[] args)
{
for (int i = 0; i < numThreads; i++)
{
Thread mycorner = new Thread(new ThreadStart(ThreadProcess));
mycorner.Name = String.Format("Thread{0}", i +
1);
mycorner.Start();
}
Console.Read();
}
}
}
Its very good explanation
ReplyDeleteThanks Prashant :p
DeleteThis comment has been removed by the author.
ReplyDelete