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.

Slides:



Advertisements
Similar presentations
Operating Systems Semaphores II
Advertisements

How to Build Multi- threaded Applications in.NET Mazen S. Alzogbi Technology Specialist Microsoft Corporation.
1 Chapter 5 Concurrency: Mutual Exclusion and Synchronization Principals of Concurrency Mutual Exclusion: Hardware Support Semaphores Readers/Writers Problem.
Ch 7 B.
Chapter 6: Process Synchronization
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
EEE 435 Principles of Operating Systems Interprocess Communication Pt II (Modern Operating Systems 2.3)
Process Synchronization. Module 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores.
CH7 discussion-review Mahmoud Alhabbash. Q1 What is a Race Condition? How could we prevent that? – Race condition is the situation where several processes.
Operating Systems CMPSC 473 Mutual Exclusion Lecture 13: October 12, 2010 Instructor: Bhuvan Urgaonkar.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
Concurrent Processes Lecture 5. Introduction Modern operating systems can handle more than one process at a time System scheduler manages processes and.
Chapter 6: Process Synchronization. Outline Background Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores Classic Problems.
Synchronization Principles. Race Conditions Race Conditions: An Example spooler directory out in 4 7 somefile.txt list.c scores.txt Process.
A Introduction to Computing II Lecture 18: Concurrency Issues Fall Session 2000.
Concurrency: Mutual Exclusion, Synchronization, Deadlock, and Starvation in Representative Operating Systems.
Monitors CSCI 444/544 Operating Systems Fall 2008.
Synchronization in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Java ThreadsGraphics Programming Graphics Programming: Java Threads.
Process Synchronization Ch. 4.4 – Cooperating Processes Ch. 7 – Concurrency.
Operating Systems CSE 411 CPU Management Oct Lecture 13 Instructor: Bhuvan Urgaonkar.
Parallel Processing (CS526) Spring 2012(Week 8).  Thread Status.  Synchronization in Shared Memory Programming(Java threads ) ◦ Locks ◦ Barriars.
Windows Programming Using C# Threading. 2 Contents Threading Thread class Interlocked class Monitor class Semaphore class Thread Pools.
Object Oriented Analysis & Design SDL Threads. Contents 2  Processes  Thread Concepts  Creating threads  Critical sections  Synchronizing threads.
Threads. Java Threads A thread is not an object A thread is a flow of control A thread is a series of executed statements A thread is a nested sequence.
12/1/98 COP 4020 Programming Languages Parallel Programming in Ada and Java Gregory A. Riccardi Department of Computer Science Florida State University.
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
1 Tutorial: CSI 3310 Dewan Tanvir Ahmed SITE, UofO.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Introduction to Concurrency.
Multithreading : synchronization. Avanced Programming 2004, Based on LYS Stefanus’s slides slide 4.2 Solving the Race Condition Problem A thread must.
6.3 Peterson’s Solution The two processes share two variables: Int turn; Boolean flag[2] The variable turn indicates whose turn it is to enter the critical.
1 Announcements The fixing the bug part of Lab 4’s assignment 2 is now considered extra credit. Comments for the code should be on the parts you wrote.
1 Concurrency Architecture Types Tasks Synchronization –Semaphores –Monitors –Message Passing Concurrency in Ada Java Threads.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Mutual Exclusion.
Internet Software Development Controlling Threads Paul J Krause.
CSC321 Concurrent Programming: §5 Monitors 1 Section 5 Monitors.
Concurrent Programming and Threads Threads Blocking a User Interface.
Laboratory - 4.  Threading Concept  Threading in.NET  Multi-Threaded Socket  Example.
1 Program5 Due Friday, March Prog4 user_thread... amount = … invoke delegate transact (amount)... mainThread... Total + = amount … user_thread...
1 G53SRP: Java Concurrency Control (1) - synchronisation Chris Greenhalgh School of Computer Science.
Synchronization Producer/Consumer Problem. Synchronization - PC with Semaphores2 Abstract The producer/consumer problem is a classical synchronization.
Chapter 7 -1 CHAPTER 7 PROCESS SYNCHRONIZATION CGS Operating System Concepts UCF, Spring 2004.
OPERATING SYSTEMS Frans Sanen.  Recap of threads in Java  Learn to think about synchronization problems in Java  Solve synchronization problems in.
Upcoming Presentations ILM Professional Service – Proprietary and Confidential ( DateTimeTopicPresenter March PM Distributed.
Java Thread and Memory Model
Threads Doing Several Things at Once. Threads n What are Threads? n Two Ways to Obtain a New Thread n The Lifecycle of a Thread n Four Kinds of Thread.
Dr. R R DOCSIT, Dr BAMU. Basic Java :Multi Threading Cont. 2 Objectives of This Session Explain Synchronization in threads Demonstrate use of.
Concurrency Control 1 Fall 2014 CS7020: Game Design and Development.
Monitors and Blocking Synchronization Dalia Cohn Alperovich Based on “The Art of Multiprocessor Programming” by Herlihy & Shavit, chapter 8.
Multi-Threading in Java
Operating Systems CSE 411 CPU Management Dec Lecture Instructor: Bhuvan Urgaonkar.
Rensselaer Polytechnic Institute CSCI-4210 – Operating Systems David Goldschmidt, Ph.D.
Threads. Objectives You must be able to answer the following questions –What code does a thread execute? –What states can a thread be in? –How does a.
1 Dr.A.Srinivas PES Institute of Technology Bangalore, India
Threads in Java Threads Introduction: After completing this chapter, you will be able to code your own thread, control them efficiently without.
1 Processes and Threads Part II Chapter Processes 2.2 Threads 2.3 Interprocess communication 2.4 Classical IPC problems 2.5 Scheduling.
1 5-High-Performance Embedded Systems using Concurrent Process (cont.)
CSC CSC 143 Threads. CSC Introducing Threads  A thread is a flow of control within a program  A piece of code that runs on its own. The.
Concurrency in Java MD. ANISUR RAHMAN. slide 2 Concurrency  Multiprogramming  Single processor runs several programs at the same time  Each program.
Concurrent Programming in Java Based on Notes by J. Johns (based on Java in a Nutshell, Learning Java) Also Java Tutorial, Concurrent Programming in Java.
Background on the need for Synchronization
Critical sections, locking, monitors, etc.
Multithreading.
Multithreading.
Threading using C# and .Net
Dr. Mustafa Cem Kasapbaşı
Another Means Of Thread Synchronization
Multithreading Dr. John P. Abraham.
CSE 542: Operating Systems
Operating Systems {week 10}
Presentation transcript:

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 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?

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 }

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)

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?

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

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);}

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*/ }

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.

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.

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

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