ProgrammingWithThreads - AnIntrotoProgrammingwithC# Threads

Info iconThis preview shows pages 1–13. Sign up to view the full content.

View Full Document Right Arrow Icon
Click to edit Master subtitle style   An Intro to Programming with C#  Threads Presentation by: Jason Bender, Garrett Lund,  Ben Gamble, Michael Calvo, and  Jeff Corbell 
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
  Outline Introduction The Basics Why use Concurrency The Design of a Thread Facility Using Locks: Accessing Shared Data Using Wait and Pulse: Scheduling Shared Resources Using Threads: Working in Parallel Using Interrupt: Diverting the Flow of Control
Background image of page 2
  Threads: The Basics What is a thread? Threads allow you to write programs with  simultaneous points of execution, synchronizing  through shared memory. Threads are lightweight Because thread creation, existence, destruction, and  synchronization primitives are cheap, programmers  will use them for all their concurrency needs.
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
  Single-threaded vs. Multithreaded
Background image of page 4
  Why use concurrency? Use of multiprocessors Driving slow devices Disks, networks, terminals, printers Human users need concurrency Distributed systems Reduce Latency
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
  The Design of a Thread Facility Four Major Mechanisms 1. Thread Creation 2. Mutual Exclusion 3. Waiting for Events 4. Getting out of unwanted long-term wait
Background image of page 6
  Thread Creation In C# you create a thread by: 1. Creating an object of type “Thread” 2. Giving its constructor a “ThreadStart” delegate 3. Calling the thread’s “Start” method Once Run method is called: 1.  Starts executing asynchronously with invocation of  delegate's method
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
  Code Example Thread t = new Thread(new ThreadStart(foo.A)); t.Start(); foo.B(); t.Join();
Background image of page 8
  Using Locks
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
   Mutual Exclusion Used to avoid errors with multiple threads  accessing shared variables Using locks is the simplest tool to accomplish this You must only access data from a thread that is  holding that lock
Background image of page 10
  Using Locks (in C#) General Form:  lock(expression) { embedded-statement(s) }
Background image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
  Locking instance fields of an object class KV {  string k, v;   public void SetKV(string newk, string newv)     {       lock (this)  {  this.k = newk;  this.v = newv; }
Background image of page 12
Image of page 13
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 07/30/2011 for the course COP 4600 taught by Professor Montagne during the Spring '08 term at University of Central Florida.

Page1 / 40

ProgrammingWithThreads - AnIntrotoProgrammingwithC# Threads

This preview shows document pages 1 - 13. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online