Docsity

Notes The Problem with Threads - Advance Parallels Programming | CISC 879, Study notes of Computer Science

Partial preview of the text

Download Notes The Problem with Threads - Advance Parallels Programming | CISC 879 and more Study notes Computer Science in PDF only on Docsity!

Author – Edward A Lee

Presented by - Varun Notibala

Dept of Computer & Information Sciences University of Delaware The Problem with Threads

Threads

  • Thread : single sequential flow of control
    • Model for concurrent programming
  • Sequential computation
    • Understandable, predictable, deterministic
  • Threads result in loss of these features
  • Methods to limit / eliminate these losses available
    • But, not effective

Problem w/ threads – WHY?

  • Concurrent abstractions chosen for threads
    • Don’t represent concurrency in real world
  • Concurrent programming using threads hard
  • MAIN IDEA Replace sequence of steps with community of interacting entities

Some terminology

  • Determinacy
  • Predictability
  • Reliability
  • Pruning

Example - Observer Pattern

public class ValueHolder { private List listeners = new LinkedList(); private int value; public interface Listener { public void valueChanged(int newValue); } public void addListener(Listener listener) { listeners.add(listener); }

Example - Observer Pattern

public class ValueHolder { private List listeners = new LinkedList(); private int value; public interface Listener { public void valueChanged(int newValue); } public void addListener(Listener listener) { listeners.add(listener); } public void setValue(int newValue) { value = newValue; Iterator i = listeners.iterator(); while(i.hasNext()) { ((Listener)i.next()) .value Changed(newValue); }

Example - Observer Pattern

public class ValueHolder { private List listeners = new LinkedList(); private int value; public interface Listener { public void valueChanged(int newValue); } public synchronized void addListener(Listener listener) { listeners.add(listener); } public void setValue(int newValue) { List copyOfListeners; synchronized(this) { value = newValue; copyOfListeners = new LinkedList(listeners); } Iterator i = copyOfListeners.iterator() ; while(i.hasNext()) { ((Listener)i.next()).value Changed(newValue); }

Observer pattern - Inference

  • Prone to deadlocks
  • Concurrent modification clash
  • Most multithreaded programs have concurrency issues
  • Multi-core architectures
    • Will concurrency bugs might show up more often ??

Aggressive pruning

  • Design patterns – transactional design
    • Work on a data copy followed by commit or abort
    • Transactions suited for intrinsic non- deterministic interactions
    • But not to build determinate concurrent interactions
  • Patterns encapsulated into libraries – concurrent data structures in Java 5.

Aggressive pruning

  • Extension of languages with keywords - Split-C, Cilk
  • Gauva language constrains java to prohibit access of unsynchronised objects from multiple threads
  • Explicit separation of read and write locks
  • Promises
  • Formal program analysis like Blast and Intel thread checker to identify potential concurrency bugs

Alternatives to threads

Observer pattern implemented using rendezvous based coordination language

  • Each component is a process
  • Merge block specifies a conditional rendezvous
  • Two possible three way rendezvous interactions

Alternatives to threads

  • The merge block represents the co-ordination language
  • This handles the synchronization
  • It can be implemented using other design models –
    • PN Director – Kahn process network for concurrency
    • SR Director – Synchronous reactive director

Overcoming non-

determinism

Deterministic interleaving using rendezvous

Alternatives to threads -

Inference

  • Split design into deterministic stand alone blocks
  • Introduce non-determinism judiciously
  • This style of concurrency is AKA “actor oriented” design - Supplement of control flow oriented design
  • Some examples of actor oriented design
    • OpenMP
    • MPI