Lecture 21: Concurrency in Other Environments (Part 2)

Similar documents
Lecture 10: Introduction to Semaphores

Processes and Threads. Industrial Programming. Processes and Threads (cont'd) Processes and Threads (cont'd)

Systems Programming & Scripting

DAD Lab. 2 Additional C# Topics

Lecture 9: Introduction to Monitors

Problems with Concurrency. February 19, 2014

Lecture 8: Threads. Lisa (Ling) Liu

Monitors & Condition Synchronization

Virtual Machine Design

Java Monitors. Parallel and Distributed Computing. Department of Computer Science and Engineering (DEI) Instituto Superior Técnico.

Java Threads. COMP 585 Noteset #2 1

Threads are lightweight processes responsible for multitasking within a single application.

Multithreaded Programming

Exercise Session Week 8

Threads Chate Patanothai

Java Threads. Written by John Bell for CS 342, Spring 2018

Exercise Session Week 8

Agenda. Highlight issues with multi threaded programming Introduce thread synchronization primitives Introduce thread safe collections

Chair of Software Engineering. Java and C# in depth. Carlo A. Furia, Marco Piccioni, Bertrand Meyer. Java: concurrency

CSCI 5828: Foundations of Software Engineering

Multiple processes can run in parallel on a single computer and multiple threads can run in parallel in a single process.

Game Engineering: 2D

Quiz on Tuesday April 13. CS 361 Concurrent programming Drexel University Fall 2004 Lecture 4. Java facts and questions. Things to try in Java

Chair of Software Engineering. Java and C# in Depth. Prof. Dr. Bertrand Meyer. Exercise Session 8. Nadia Polikarpova

Operating Systems. Operating Systems Summer 2017 Sina Meraji U of T

Monitors & Condition Synchronization

7. MULTITHREDED PROGRAMMING

Faculty of Computers & Information Computer Science Department

Monitors & Condition Synchronisation

Programmazione Avanzata e Paradigmi Ingegneria e Scienze Informatiche - UNIBO a.a 2013/2014 Lecturer: Alessandro Ricci

Monitors; Software Transactional Memory

CS61B, Spring 2003 Discussion #17 Amir Kamil UC Berkeley 5/12/03

CMSC 330: Organization of Programming Languages

Monitors; Software Transactional Memory

C# Syllabus. MS.NET Framework Introduction

Concurrent Programming using Threads

Overview. CMSC 330: Organization of Programming Languages. Concurrency. Multiprocessors. Processes vs. Threads. Computation Abstractions

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

Multithread Computing

Course Hours

Multitasking Multitasking allows several activities to occur concurrently on the computer. A distinction is usually made between: Process-based multit

Chapter 32 Multithreading and Parallel Programming

.Net. Course Content ASP.NET

Concurrency - Topics. Introduction Introduction to Subprogram-Level Concurrency Semaphores Monitors Message Passing Java Threads

EEE-425 Programming Languages (2013) 1

JAVA and J2EE UNIT - 4 Multithreaded Programming And Event Handling

CSE332: Data Abstractions Lecture 22: Shared-Memory Concurrency and Mutual Exclusion. Tyler Robison Summer 2010

Modern Programming Languages. Lecture Java Programming Language. An Introduction

INTRODUCTION TO.NET. Domain of.net D.N.A. Architecture One Tier Two Tier Three Tier N-Tier THE COMMON LANGUAGE RUNTIME (C.L.R.)

Creating and Running Your First C# Program

EEE-425 Programming Languages (2013) 1

COMP346 Winter Tutorial 4 Synchronization Semaphores

Chapter 13 Working with Threads

Contents. 6-1 Copyright (c) N. Afshartous

Creating a Service Platform for.net. Clement Escoffier PhD Student University of Grenoble

Week 7. Concurrent Programming: Thread Synchronization. CS 180 Sunil Prabhakar Department of Computer Science Purdue University

Part IV Other Systems: I Java Threads

Lecture 7: Process & Thread Introduction

C#: framework overview and in-the-small features

User Space Multithreading. Computer Science, University of Warwick

MultiThreading. Object Orientated Programming in Java. Benjamin Kenwright

COMPSCI 230 Threading Week8. Figure 1 Thread status diagram [

INF 212 ANALYSIS OF PROG. LANGS CONCURRENCY. Instructors: Crista Lopes Copyright Instructors.

Advanced Programming C# Lecture 13. dr inż. Małgorzata Janik

Threads Questions Important Questions

COMP31212: Concurrency A Review of Java Concurrency. Giles Reger

Concurrency User Guide

CS11 Java. Fall Lecture 7

COE518 Lecture Notes Week 7 (Oct 17, 2011)

Unit - IV Multi-Threading

Module - 4 Multi-Threaded Programming

CS 159: Parallel Processing

Programming Language Concepts: Lecture 11

Parallel Programming Languages COMP360

Programming in Parallel COMP755

Crash Course in Java. Why Java? Java notes for C++ programmers. Network Programming in Java is very different than in C/C++

CS360 Lecture 12 Multithreading

Threads & Networking

Multithreading using Java. Dr. Ferdin Joe John Joseph

Java s Implementation of Concurrency, and how to use it in our applications.

PES INSTITUTE OF TECHNOLOGY

Only one thread can own a specific monitor

Concurrency with Threads and Actors

Writing Parallel Programs COMP360

DC69 C# &.NET JUNE C# is a simple, modern, object oriented language derived from C++ and Java.

CSE 332: Data Structures & Parallelism Lecture 17: Shared-Memory Concurrency & Mutual Exclusion. Ruth Anderson Winter 2019

Advanced Programming C# Lecture 12. dr inż. Małgorzata Janik

Chapter 4: Multi-Threaded Programming

CS193k, Stanford Handout #8. Threads 3

Parallel & Concurrent Programming

CS 2112 Lecture 20 Synchronization 5 April 2012 Lecturer: Andrew Myers

Multithreading. Multithreading. HIS CHAPTER DISCUSSES how to write multithreaded code. To do this, Another Thread

Exercises and Labs. Part I. Exercises

Introduction to Java Threads

Multithreaded Programming

CMSC 132: Object-Oriented Programming II

Programming C# 5.0. Ian Griffiths O'REILLY' Beijing Cambridge * Farnham Kbln Sebastopol Tokyo

An Introduction to Programming with Java Threads Andrew Whitaker University of Washington 9/13/2006. Thread Creation

Core JAVA Training Syllabus FEE: RS. 8000/-

Synchronization synchronization.

Transcription:

COMP 150-CCP Concurrent Programming Lecture 21: Concurrency in Other Environments (Part 2) Dr. Richard S. Hall rickhall@cs.tufts.edu Clement Escoffier clement.escoffier@gmail.com Concurrent programming April 10th, 2008

Agenda.NET and C#

What is.net? The.NET Framework is Microsoft's managed code programming model for building applications on Windows clients, servers, and mobile or embedded devices. Developers use.net to build applications of many types: Web applications, server applications, smart client applications, console applications, database applications, and more.

What is.net? VB.NET C# J# Managed C++ Cobol.Net Web Forms, Web Services, ASP.NET Windows Forms.NET Framework Library Common Language Runtime (CLR) Win

What is.net? The CLR is a virtual machine Support several programming languages Interpreting Intermediate Language (MSIL) Close to Java on a lot of points.net provides a lot of library to create Windows GUI Web Applications Windows Vista uses a lot the.net framework

Inside the CLR Base Class Library Exception Manager Thread Support COM Marshaller Type Checker Debug Engine Code Manager JIT Compiler (IL => Native) Garbage Collector Class Loader

In this Lecture How to handle multi-threading in C#? C# is the most famous.net language Very close to Java Between C++ and Java How to create threads How to use monitors How to use condition variables

Using Thread in C# Import the System.Threading namespace using System.Threading; Equivalent to packages in Java

Creating a Thread Two steps: First create a ThreadStart object (delegate) In argument, give the pointer on the function to call ThreadStart ts = new ThreadStart(<function name>); No Runnable interface to implement Then, create the Thread object Thread newthread = new Thread(ts); To Start the Thread, call the start method: newthread.start();

Thread Creation Example using System; using System.Threading; class Test { static void Main() { Work w = new Work(); w.data = 42; ThreadStart threaddelegate = new ThreadStart(w.DoWork); Thread newthread = new Thread(threadDelegate); newthread.start(); class Work { public int Data; public void DoWork() { Console.WriteLine("Instance thread procedure. Data={0", Data);

The Simplest Way (with Argument) public static void Main() { Thread newthread = new Thread(Work.DoWork); newthread.start(42); Work w = new Work(); newthread = new Thread(w.DoWork); // Pass an object containing data for the thread. newthread.start(42); public void DoMoreWork(object data) { Console.WriteLine("Instance thread procedure. Data='{0'", data);

Waiting for Thread Termination Two Join methods are available on the Thread object Join() Blocks the calling thread until a thread terminates Join(Int32) Blocks the calling thread until a thread terminates or the specified time elapses

Waiting for Thread Termination An event base system is also available A thread waits until another thread has send an event Several types of event and wait methods are availble.

Waiting for Thread Termination static void Main() { AutoResetEvent autoevent = new AutoResetEvent(false); Thread regularthread = new Thread(new ThreadStart(ThreadMethod)); regularthread.start(); ThreadPool.QueueUserWorkItem(new WaitCallback(WorkMethod), autoevent); // Wait for foreground thread to end. regularthread.join(); // Wait for background thread to end. autoevent.waitone(); static void ThreadMethod() { Console.WriteLine("ThreadOne, executing ThreadMethod, " + "is {0from the thread pool.", Thread.CurrentThread.IsThreadPoolThread? "" : "not "); static void WorkMethod(object stateinfo) { Console.WriteLine("ThreadTwo, executing WorkMethod, " + "is {0from the thread pool.", Thread.CurrentThread.IsThreadPoolThread? "" : "not "); // Signal that this thread is finished. ((AutoResetEvent)stateInfo).Set();

Suspending/Resuming a Thread As in Java, the Suspend and Resume method are obsolete

Using Monitors C# provides a keyword equivalent to the Java synchronized keyword Lock(object) This keyword can t be use on method signature Only in the method code Lock are re-entrant in C#

Using Monitors int Withdraw(int amount) { if (balance < 0) { throw new Exception("Negative Balance"); lock (this) { if (balance >= amount) { Console.WriteLine("Balance before Withdrawal : " + balance); Console.WriteLine("Amount to Withdraw : -" + amount); balance = balance - amount; Console.WriteLine("Balance after Withdrawal : " + balance); return amount; else { return 0; // transaction rejected

Using Monitors: The Monitor Class Locks are mapped on a static class: the Monitor Class The lock statement is implemented by using the Enter and Exit methods of the Monitor class, and it uses try catch finally to ensure that the lock is released. A Monitor object maintain A reference to the thread that currently holds the lock. A reference to a ready queue, which contains the threads that are ready to obtain the lock. A reference to a waiting queue, which contains the threads that are waiting for notification of a change in the state of the locked object.

Using Monitors: The Monitor Class Enter, TryEnter Acquires a lock for an object. This action also marks the beginning of a critical section. Wait Releases the lock on an object in order to permit other threads to lock and access the object. Pulse, PulseAll Sends a signal to one or more waiting threads. The signal notifies a waiting thread that the state of the locked object has changed, and the owner of the lock is ready to release the lock. Exit Releases the lock on an object. This action also marks the end of a critical section protected by the locked object.

Using Monitors: The Monitor Class int Withdraw(int amount) { if (balance < 0) { throw new Exception("Negative Balance"); Monitor.Enter(this); if (balance >= amount) { Console.WriteLine("Balance before Withdrawal : " + balance); Console.WriteLine("Amount to Withdraw : -" + amount); balance = balance - amount; Console.WriteLine("Balance after Withdrawal : " + balance); return amount; else { return 0; // transaction rejected Monitor.Exit(this); This is not correct!

Using Monitors: The Monitor Class int Withdraw(int amount) { if (balance < 0) { throw new Exception("Negative Balance"); Monitor.Enter(this); if (balance >= amount) { Console.WriteLine("Balance before Withdrawal : " + balance); Console.WriteLine("Amount to Withdraw : -" + amount); balance = balance - amount; Console.WriteLine("Balance after Withdrawal : " + balance); return amount; else { Monitor.Exit(this); return 0; // transaction rejected Monitor.Exit(this); This is correct!

Monitors & Condition Variables The Monitor class specify Different wait method Wait(Object) Releases the lock on an object and blocks the current thread until it reacquires the lock. Wait(Object, Int32) Releases the lock on an object and blocks the current thread until it reacquires the lock. If the specified time-out interval elapses, the thread enters the ready queue. Wait(Object, Int32, Boolean) Releases the lock on an object and blocks the current thread until it reacquires the lock. If the specified time-out interval elapses, the thread enters the ready queue. This method also specifies whether the synchronization domain for the context (if in a synchronized context) is exited before the wait and reacquired afterward.

Monitors & Condition Variables The Monitor class specify Different notify method Pulse Notifies a thread in the waiting queue of a change in the locked object's state. PulseAll Notifies all waiting threads of a change in the object's state.

C# Bounded Buffer class LockedBuffer : Buffer { private Object[] buf; private int count = 0; private int input = 0; private int output = 0; // public LockedBuffer(int size) { buf = new Object[size];

C# Bounded Buffer public void Put(Object o) { lock (this) { while (count == buf.length) { try { Monitor.Wait(this); catch (SynchronizationLockException e) { Console.WriteLine(e); return; catch (ThreadInterruptedException e) { Console.WriteLine(e); return; Console.WriteLine("PUT "); for (int i = 0; i < count; i++) { Console.WriteLine(" " + buf[(i + output) % buf.length] + " "); Console.WriteLine("[" + o + "]"); buf[input] = o; ++count; input = (input + 1) % buf.length; Monitor.PulseAll(this);

C# Bounded Buffer public Object Get() { lock (this) { while (count == 0) { try { Monitor.Wait(this); catch (SynchronizationLockException e) { Console.WriteLine(e); return null; catch (ThreadInterruptedException e) { Console.WriteLine(e); return null; Object o = buf[output]; buf[output] = null; --count; output = (output + 1) % buf.length; Monitor.PulseAll(this); Console.WriteLine("GET [" + o + "] "); for (int i = 0; i < count; i++) { Console.WriteLine(buf[(i + output) % buf.length] + " "); Console.WriteLine(""); return o;

C# Bounded Buffer public class Producer { private Buffer buf; private String alphabet = "abcdefghijklmnopqrstuvwxyz"; private bool paused = true; public Producer(Buffer b) { buf = b; public void Pause() { lock (this) { paused =!paused; Monitor.PulseAll(this); public void Run() { int ai = 0; while (true) { lock (this) { while (paused) { Monitor.Wait(this); buf.put((alphabet.tochararray()[ai])); ai = (ai + 1) % alphabet.length; Thread.Sleep(500);

C# Bounded Buffer public class Consumer { private Buffer buf; private bool paused = true; public Consumer(Buffer b) { buf = b; public void Pause() { lock(this) { paused =!paused; Monitor.PulseAll(this); public void Run() { while (true) { lock (this) { while (paused) { Monitor.Wait(this); Object o = buf.get(); Thread.Sleep(500);

C# Bounded Buffer static void Main(string[] args) { Buffer buffer = new LockedBuffer(5); Producer prod = new Producer(buffer); Consumer cons = new Consumer(buffer); Thread producer = new Thread(new ThreadStart(prod.Run)); Thread consumer = new Thread(new ThreadStart(cons.Run)); producer.start(); consumer.start(); Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Gui(prod, cons));

Other Synchronization Mechanisms The Managed Thread Pool Timers Wait Handles EventWaitHandle, AutoResetEvent, and ManualResetEvent Mutexes Interlocked Operations Reader-Writer Locks Semaphores

Conclusion.NET provided synchronization mechanisms very similar to Java It provides almost the same class than the Java Concurrent API These classes are supported on Compact.NET All synchronization classes are usable in any others.net languages Except the lock macro