Presentation is loading. Please wait.

Presentation is loading. Please wait.

C# I 1 CSC 298 Threads. C# I 2 Introducing Threads  A thread is a flow of control within a program  A piece of code that runs on its own. The execution.

Similar presentations


Presentation on theme: "C# I 1 CSC 298 Threads. C# I 2 Introducing Threads  A thread is a flow of control within a program  A piece of code that runs on its own. The execution."— Presentation transcript:

1 C# I 1 CSC 298 Threads

2 C# I 2 Introducing Threads  A thread is a flow of control within a program  A piece of code that runs on its own. The execution environment is the same as for the rest of the program. The thread can share data with other parts (=other threads) of the program.  A thread executes concurrently with the other threads of the program.  e.g. when painting (or repainting), the method OnPaint is executed by a thread.  Key issue: synchronization  If the threads run concurrently and share data, what happens when one thread uses data modified at the same time by another thread?

3 C# I 3 The Thread class  in System.Threading  Create a thread by instantiating the Thread class  Need a ThreadStart object: a delegate to define the method run by the thread. ThreadStart ts = new ThreadStart(Foo); // Note: can attach several methods to // a thread // e.g. ts += new ThreadStart(Bar); MyThread t = new Thread(ts); t.Start(); private void Foo() { // Code for the thread }

4 C# I 4 Life of a thread  When start is called, the thread t becomes ready to be executed by the processor.  When another thread gets a turn, t will stop executing its method. It will start again from where it left off, when it gets another turn  The scheduling of the threads is the responsibility of the OS  However, we can temporarily stop the execution, e.g. using the methods Sleep and Suspend (see the examples to come)

5 C# I 5 Example  Create 2 threads that write their names and sleep  see the full code on the class web site: ThreadExample.cs  Can you predict the order of execution?

6 C# I 6 Concurrent threads  When 2 threads running independently can modify the same data, they need to cooperate.  If not, the state of the data is unpredictable  See an example : BadConcurrency.cs on the class web site.  To avoid the problem, synchronize the two threads,e.g. in BadConcurrency.cs write for Thread1: Monitor.Enter(data); data[0]=1; data[1]=1; // etc... Monitor.Exit(data); // The thread must have an exclusive // access to data before entering the // Monitor block

7 C# I 7 Marking a critical section  Use the Monitor class  What can be marked?  A block of C# code (as in the previous example). Monitor.Enter(myObject) /*block of code*/ Monitor.Exit(myObject)  The thread needs a lock on myObject before entering the block of code.  To lock an entire instance method, use a lock on this. public void update(){ // need a lock on this Monitor.Enter(this); // to execute update /*code*/ Monitor.Exit(this);}

8 C# I 8 lock keyword  To mark a critical section (same role as Monitor.Enter and Monitor.Exit)  This code Monitor.Enter(myObject) /*block of code*/ Monitor.Exit(myObject)  can be written as lock(myObject){ /*block of code*/ }

9 C# I 9 Wait method  available within the Monitor class  inside of a critical section, a thread can give up its hold on a lock by calling Wait (within the Monitor class). Then, another thread can take the lock.  When the first thread reacquires its lock, it starts from where it left off.  Reacquisition is not assured. The thread must wake up. This is done if another thread calls Pulse() or PulseAll(). Then the thread is requeued and competes for the lock with the other threads.

10 C# I 10 Example  A Sender sends items to a Receiver via a Buffer  Both the Sender and the Receiver can modify the Buffer content.  The access to the Buffer must be synchronized if Sender and Receiver are two independent threads.  See TestSenderReceiver.cs (and other files) on the class web site.

11 C# I 11 deadlock  When threads wait for locks that can't be freed.  e.g. in the Sender-Receiver example, there is deadlock if  The Sender sends only to an empty buffer  The Receiver takes only from a full buffer

12 C# I 12 Animation  Animation is better achieved with threads  Without an animation thread, one part of the program can hold up the animation part.  Example: BouncingBall.cs


Download ppt "C# I 1 CSC 298 Threads. C# I 2 Introducing Threads  A thread is a flow of control within a program  A piece of code that runs on its own. The execution."

Similar presentations


Ads by Google