Académique Documents
Professionnel Documents
Culture Documents
Net
using System; Threading
using System.Threading;
public class Test
{
public static void Main(string[] ar)
{
Thread t1= new Thread(new ThreadStart(Display1));
Thread t2= new Thread(new ThreadStart(Display2));
t1.start();
t2.start();
}
public static void Display1()
{
for(int i=0; i<20; i++)
{
Console.WriteLine(i);
}
}
public static void Display2()
{
for(int i=0; i<20; i++)
{
Console.WriteLine(“Welcome”);
}
}
}
Threads :
• Thread is the fundamental unit of execution.
thread.Start();
//starts the thread running
Thread Priorities :
Controls the amount of CPU time that can be allotted to a thread.
ThreadPriority.Highest
ThreadPriority.AboveNormal
ThreadPriority.Normal
ThreadPriority.BelowNormal
ThreadPriority.Lowest
Inter-thread Communication
Thread lifecycle
Running Suspended
Unstarted Aborted
WaitSleepJoin
Inter-thread Communication
Thread lifecycle
Running
8
C# Threading
Thread Methods
• Start()
▫ Begins execution of the thread
▫ Once a thread is finished, it cannot be restarted
• Suspend() Thread.Suspend
▫ Suspends the thread
▫ If the thread is already suspended, there is no effect
• Resume() Thread.Resume
▫ Resumes a suspended thread
• Interrupt()
▫ Resumes a thread that is in a WaitSleepJoin state
▫ If the thread is not in WaitSleepJoin state it will be interrupted next time it is blocked
9
C# Threading
• Join()
▫ Blocks the calling thread until the owning thread terminates
• A thread can prevent itself from being terminated
with Thread.ResetAbort.
try{
…
}
catch(ThreadAbortException)
{
Thread.ResetAbort();
}
• Thread.Join()
▫ When one thread terminates another, wait for the
other thread to end.
• Thread Synchronization :
▫ Threads must be coordinated to prevent data
corruption.
• Monitors
▫ Monitors allow us to obtain a lock on a particular
object and use that lock to restrict access to critical
section of code.
}
Thread Synchronization
using System.Threading;
namespace CSharpThreadExample
{
class Program
{ static void Main(string[] arg)
{ Console.WriteLine("*****Multiple Threads*****");
Printer p=new Printer();
Thread[] Threads=new Thread[3];
for(int i=0;i<3;i++)
{ Threads[i]=new Thread(new ThreadStart(p.PrintNumbers));
Threads[i].Name="Child "+i; }
foreach(Thread t in Threads)
t.Start();
Console.ReadLine();
} }
class Printer
{ public void PrintNumbers()
{ for (int i = 0; i < 5; i++)
{ Thread.Sleep(100);
Console.Write(i + ",");
}
Console.WriteLine();
} }
}
Thread Synchronization
public void PrintNumbers()
{ Monitor.Enter(this);
try
{ for (int i = 0; i < 5; i++)
{ Thread.Sleep(100);
Console.Write(i + ",");
}
Console.WriteLine();
}
finally
{ Monitor.Exit(this);
} }
Synchronization
Using a lock Block
SyncLock
• Example: lock block in C# In VB.NET
lock (this)
{
workQueue[nextFreeElement] = unit;
nextFreeElement++; } Critical
code
}
lock(buffer)
{ ……. }
is equivalent to
Monitor.Enter(buffer);
try
{ critical section; }
finally
{ Monitor.Exit(buffer); }
- Also ensures the presence of a finally block to make sure the lock is released.
Synchronization
Obtaining Locks with Monitors
Monitor.Enter(this);
workQueue[nextFreeElement] = unit;
nextFreeElement++; } Critical
code
Monitor.Exit(this);
C# Threading
Threading Dangers
• Race Conditions
▫ When multiple threads access the same memory
▫ A thread is interrupted while updating memory
▫ Solution: “lock” memory in a mutex, monitor, etc.
• Deadlock
▫ Two or more threads waiting on each other to release resources
20
C# Threading
Deadlock
• Your job to avoid it
Resource1 Resource1