C++11 threads -- a simpler interface than pthreads Examples largely taken from 013/02/24/investigating-c11-threads/

Size: px
Start display at page:

Download "C++11 threads -- a simpler interface than pthreads Examples largely taken from https://latedev.wordpress.com/ 013/02/24/investigating-c11-threads/"

Transcription

1 C++11 threads -- a simpler interface than pthreads Examples largely taken from 013/02/24/investigating-c11-threads/ #include <iostream> #include <thread> using namespace std; void printnums(int start, int n) { for( int i = 0; i < n; i++ ) { cout << "[" << start + i << "]" << endl; int main( ) { thread t1(printnums, 0, 100 );

2 An example #include <iostream> #include <thread> void printnums(int start, int n) { for( int i = 0; i < n; i++ ) { std::cout << "[" << start + i << "]" << std::endl; t1 is an object int main( ) { std::thread t1(printnums, 0, 100 ); The thread class binds the arguments 0, 100 to the function printnums

3 What happens after the thread has started? #include <iostream> #include <thread> using namespace std; void printnums(int start, int n) { for( int i = 0; i < n; i++ ) { cout << "[" << start + i << "]" << endl; int main( ) { thread t1(printnums, 0, 100 ); It is likely the thread running main terminates before t1 finishes. This leads to an undefined state since the main thread has died. This can lead to abnormal terminations on some systems.

4 Solve this by using join #include <iostream> #include <thread> using namespace std; void printnums(int start, int n) { for( int i = 0; i < n; i++ ) { cout << "[" << start + i << "]" << endl; Need to wait for t1 to finish before allowing the main thread to finish This can be done using join. int main( ) { thread t1(printnums, 0, 100 ); t1.join( );

5 Executing the program with two threads #include <iostream> #include <thread> using namespace std; void printnums(int start, int n) { for( int i = 0; i < n; i++ ) { cout << "[" << start + i << "]" << endl; [[1000]0] [1] [2] [3] [4 [1001] int main( ) { thread t1(printnums, 0, 100 ); thread t2( printnums, 1000, 100 ); t1.join(); t2.join(); Output from the two threads is interleaved because of a lack of synchronization. There is a race on the output buffer.

6 Executing with many threads #include <iostream> #include <thread> using namespace std; void printnums(int start, int n) { for( int i = 0; i < n; i++ ) { cout << "[" << start + i << "]" << endl; [[1000]0] [1] [2] [3] [4 [1001] int main( ) { thread t[50]; for (int i=0; i < 50; i++) { t[i] = thread t2( printnums, i, 100 ); for (int i=0; i< 50; i++) {t[i].join]; Output from the two threads is interleaved because of a lack of synchronization. There is a race on the output buffer.

7 mutex locks #include <iostream> #include <thread> #include <mutex> using namespace std; void outnum( int n ) { static mutex m; m.lock( ); cout << "[" << n << "]" << endl; m.unlock( ); void printnums( int start, int n ) { for( int i = 0; i < n; i++ ) { outnum( start + i ); m is an object A mutex lock class is provided. As with C Pthreads, locks are not explicitly associated with objects You need to do that. int main() { /* as before */

8 Ensuring locks are released #include <iostream> #include <thread> #include <mutex> using namespace std; void outnum( int n ) { static mutex m; m.lock( ); cout << "[" << n << "]" << endl; m.unlock( ); void printnums( int start, int n ) { for( int i = 0; i < n; i++ ) { outnum( start + i ); int main() { /* as before */ What happens when a lock is acquired and an exception or some other event causes the lock not to be released? What happens if a lock goes out of scope and is deleted without being released? In both cases, the lock is never released.

9 Ensuring locks are released... #include <iostream> #include <thread> #include <mutex> using namespace std; void outnum( int n ) { static mutex m; lock_guard <mutex> lock( m ); cout << "[" << n << "]" << endl; void printnums( int start, int n ) { for( int i = 0; i < n; i++ ) { outnum( start + i ); What needs to happen is that a helper class that wraps the lock needs to be created. In the delete function for this helper class the lock is released. C++11 provides this in the form of lock_guards. int main() { /* as before */

10 lock_guards #include <iostream> #include <thread> #include <mutex> using namespace std; void outnum( int n ) { static mutex m; lock_guard <mutex> lock( m ); cout << "[" << n << "]" << endl; void printnums( int start, int n ) { for( int i = 0; i < n; i++ ) { outnum( start + i ); int main() { /* as before */ lock: acquires the lock if available, goes on a queue if not lock_guards support try_lock: doesn t go on wait queue if lock not acquired call_once allows multiple threads to call functions, only one is executed Use delete to unlock before lock_guard is destroyed.

11 call_once void call_once( std::once_flag& flag, Callable&& f, Args&&... args ); Given a bunch of threads executing call_once with the same flag, only one callable (e.g., a function) will be called. Which is called is indeterminate All call_onces wait until the the called function f completes If f throws an exception, the calling thread catches it and another threads f is called. Often used to initialize a state when multiple threads might do it.

12 sleeping void outnum( int n ) { static mutex m; lock_guard <mutex> lock( m ); cout << "[" << n << "]" << endl; void printnums( int start, int n ) { for( int i = 0; i < n; i++ ) { outnum( start + i ); this_thread::sleep_for( chrono::milliseconds( 200 ) ); int main() { /* as before */

13 What is the C++ memory model Remember that a memory model says how loads and stores from different threads interact, and when a thread are guaranteed to see stores from another threads. C++ has a much simpler memory model than Java Java allows programs with races, and tries to define the semantics of those programs. C++ says a program with a race is undefined, and its outcome is undefined. C++ has catch fire semantics. Any behavior, including your computer catching fire, is legal with a program with races

14 Why don t N threaded programs always go N times as fast?

15 Caches Processors are much faster than memory Core i7 Xeon 5500 (from sites/products/collateral/hpc/vtune/performance_analysis_guide.pdf) fastest (L1) cache ~4 cycles next fastest (L2) cache ~10 cycles next fastest (L3) cache ~40 cycles DRAM 100ns or about 300 cycles

16 Caches Core 0 Core 1 Core 2 Core 3 computation stuff computation stuff computation stuff computation stuff L1 Cache L1 Cache L1 Cache L1 Cache L2 Cache L2 Cache L2 Cache L2 Cache L3 Cache bus DRAM

17 How is memory laid out? a 2D array in memory looks like: a(0,0) a(0,1) a(0,2) a(1,0) a(1,1) a(1,2) a(2,0) a(2,1) a(2,2) When you read one word, several words are brought into cache a(0,0) a(0,1) a(0,2) a(1,0) a(1,1) a(1,2) a(2,0) a(2,1) a(2,2)

18 Accessing Arrays for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) {... = a[j,i]

19 Accessing Arrays for (int j = 0; j < n; j ++) { for (int i = 0; i < n; i ++) {... = a[j,i] loop interchange

20 Accessing Arrays for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { a[i,j] = a[j,i]

21 Accessing Arrays for (int j = 0; j < n; j ++) { for (int i = 0; i < n; i ++) { a[i,j] = a[j,i] loop interchange doesn t help

22 Tiling solves this problem This is discussed in detail in ECE 468, compilers Basically, extra loops are added to the code to allow blocks, or tiles, of the array that fit into cache to be accessed As much work as possible is done on a tile before moving to the next tile Accesses within a tile are done within the cache Because tiling changes the order elements are accessed it is not always legal to do

23 for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { a[i,j] = a[j,i] Tiling a array for (ti = 0, ti < n; ti +=64) { for (tj = 0, TJ < n; TJ+=64) { for (i = ti; i < min(ti+63, n); i++) { for (j = tj; j < min(tj+63, n); j++) { a[i][j] = b[j][i]

24 For matrix multiply, you have O(N 2 ) data and O(N 3 ) operations Ideally, you would bring O(N 2 ) data into cache Without tiling, your bring O(N 3 ) data into cache, as array elements get bounced from cache and brought back in Tiling reduces cache missing by a factor of N

25 A simple core fetch & decode instruction Consider a simple core arithmetic logic unit (ALU) -- execute instruction programmable execution context (registers, condition codes, etc.) processor managed execution context (cache, various buffers and translation tables)

26 A more realistic processor fetch & decode instruction ALU 1 ALU 2 ALU 3 vector1 vector3 vector3 programmable execution context processor managed execution context Consider a more realistic core Because programs often have multiple instructions that can execute at the same time, put in multiple ALUs to allow instruction level parallelism (ILP) Average # instructions per cycle < 2, depends on the application and the architecture.

27 Wasted resources fetch & decode instruction ALU 1 ALU 2 ALU 3 vector1 vector3 vector3 programmable execution context processor managed execution context Since the average # instructions per cycle < 2 for many applications, and the ALUs support at least 9 operations, lots of wasted resources If we could run two threads on the core, we could double the number of ALUs used, increase performance, and at little cost. This is called hyperthreading

28 There is a problem fetch & decode instruction ALU 1 ALU 2 ALU 3 vector1 vector3 vector3 programmable execution context processor managed execution context But programs (and programmers) control which registers are used. If both threads or processes executing on the core at the same time use register 0, we are doomed. And some register must be used by each thread/ program (e.g., the program counter) for the program to run

29 ... and a solution fetch & decode instruction ALU 1 ALU 2 ALU 3 vector1 vector3 vector3 programmable execution context programmable execution context processor managed execution context Duplicate the processor state that the programmer has access to. Both threads or programs running at the same time on the core have their own copy of this state Ideally, hyperthreading gives 2X performance. Can give less or even slow things down

30 A possible problem fetch & decode instruction ALU 1 ALU 2 ALU 3 vector1 vector3 vector3 programmable execution context programmable execution context processor managed execution context First: if a thread or program uses most of the ALUs, and another thread or program running at the same time on the core uses most of the ALUs, both threads run slower that they would with their own core. Matrix multiply, numerical programs, games, etc., can fall into this category.

31 fetch & decode instruction ALU 1 ALU 2 ALU 3 vector1 vector3 vector3 programmable execution context programmable execution context Part of the processor managed execution context is cache. If two threads or programs are running on the core at the same time, the cache requirements will increase, leading to more cache misses and slower execution times processor managed execution context

32 A second problem Assume we have 4 cores, and 6 threads A thread t0 gets scheduled to core 0. It later gets taken off the core to allow another thread, t5, to run. Core 2 becomes empty. Do we schedule t0 on core 2, or wait until core 0 becomes available? Doing the first requires moving much of the threads stuff in the cache of core 0 onto core 2 Doing the second has core 2 sitting idle, and thread t0 not running. Typically OS s will let a thread sit idle for a period of time before scheduling on a new core. An attempt to cut their losses

Distributed Programming

Distributed Programming Distributed Programming Lecture 02 - Processes, Threads and Synchronization Edirlei Soares de Lima Programs and Processes What is a computer program? Is a sequence

More information

PROGRAMOVÁNÍ V C++ CVIČENÍ. Michal Brabec

PROGRAMOVÁNÍ V C++ CVIČENÍ. Michal Brabec PROGRAMOVÁNÍ V C++ CVIČENÍ Michal Brabec PARALLELISM CATEGORIES CPU? SSE Multiprocessor SIMT - GPU 2 / 17 PARALLELISM V C++ Weak support in the language itself, powerful libraries Many different parallelization

More information

COMP6771 Advanced C++ Programming

COMP6771 Advanced C++ Programming 1. COMP6771 Advanced C++ Programming Week 9 Multithreading (continued) 2016 www.cse.unsw.edu.au/ cs6771 2. So Far Program Workflows: Sequential, Parallel, Embarrassingly Parallel Memory: Shared Memory,

More information

COMP6771 Advanced C++ Programming

COMP6771 Advanced C++ Programming 1.... COMP6771 Advanced C++ Programming Week 9 Multithreading 2016 www.cse.unsw.edu.au/ cs6771 .... Single Threaded Programs All programs so far this semester have been single threaded They have a single

More information

Multi-threaded Programming in C++

Multi-threaded Programming in C++ Multi-threaded Programming in C++ (2016/2017) Giuseppe Massari, Federico Terraneo giuseppe.massari@polimi.it federico.terraneo@polimi.it Outline 2/45 Introduction Multi-tasking implementations C++11 threading

More information

Memory Consistency Models

Memory Consistency Models Memory Consistency Models Contents of Lecture 3 The need for memory consistency models The uniprocessor model Sequential consistency Relaxed memory models Weak ordering Release consistency Jonas Skeppstedt

More information

C - Grundlagen und Konzepte

C - Grundlagen und Konzepte C - Grundlagen und Konzepte Threads Marcel Hellwig 1hellwig@informatik.uni-hamburg.de Universität Hamburg 5. Juli 2013 SOSE 13 cba This work is licensed under a Creative Commons Attribution-ShareAlike

More information

1a Computers, Problem Solving!

1a Computers, Problem Solving! 1a Computers, Problem Solving!! 1E3! 1a Computers and Problem Solving 1 Objectives! n To introduce the architecture of a computer.! n To introduce the notion of a programming language.! n To explore computational

More information

CAAM 420 Fall 2012 Lecture 29. Duncan Eddy

CAAM 420 Fall 2012 Lecture 29. Duncan Eddy CAAM 420 Fall 2012 Lecture 29 Duncan Eddy November 7, 2012 Table of Contents 1 Templating in C++ 3 1.1 Motivation.............................................. 3 1.2 Templating Functions........................................

More information

Concurrent programming in C++11

Concurrent programming in C++11 Concurrent programming in C++11 Computer Architecture J. Daniel García Sánchez (coordinator) David Expósito Singh Francisco Javier García Blas ARCOS Group Computer Science and Engineering Department University

More information

ECE 461 Fall 2011, Final Exam

ECE 461 Fall 2011, Final Exam ECE 461 Fall 2011, Final Exam DO NOT START WORKING ON THIS UNTIL TOLD TO DO SO. LEAVE IT ON THE DESK. You have until 9:00PM to take this exam. Your exam should have 17 pages total (including this cover

More information

Programming in Parallel COMP755

Programming in Parallel COMP755 Programming in Parallel COMP755 All games have morals; and the game of Snakes and Ladders captures, as no other activity can hope to do, the eternal truth that for every ladder you hope to climb, a snake

More information

Homework 4. Any questions?

Homework 4. Any questions? CSE333 SECTION 8 Homework 4 Any questions? STL Standard Template Library Has many pre-build container classes STL containers store by value, not by reference Should try to use this as much as possible

More information

Let s go straight to some code to show how to fire off a function that runs in a separate thread:

Let s go straight to some code to show how to fire off a function that runs in a separate thread: Introduction to C++11 Threads In C++11 a wrapper Thread class is available. However, you still need to have a library that implements the thread class. Visual Studio supports C++11 threads, but some other

More information

pointers + memory double x; string a; int x; main overhead int y; main overhead

pointers + memory double x; string a; int x; main overhead int y; main overhead pointers + memory computer have memory to store data. every program gets a piece of it to use as we create and use more variables, more space is allocated to a program memory int x; double x; string a;

More information

Concurrent Programming in C++ Venkat

Concurrent Programming in C++ Venkat Concurrent Programming in C++ Venkat Subramaniam venkats@agiledeveloper.com @venkat_s Platform Neutral The standard concurrency model makes it possible to write portable concurrent code Level of Concurrency

More information

Concurrency, Thread. Dongkun Shin, SKKU

Concurrency, Thread. Dongkun Shin, SKKU Concurrency, Thread 1 Thread Classic view a single point of execution within a program a single PC where instructions are being fetched from and executed), Multi-threaded program Has more than one point

More information

CS 31: Introduction to Computer Systems : Threads & Synchronization April 16-18, 2019

CS 31: Introduction to Computer Systems : Threads & Synchronization April 16-18, 2019 CS 31: Introduction to Computer Systems 22-23: Threads & Synchronization April 16-18, 2019 Making Programs Run Faster We all like how fast computers are In the old days (1980 s - 2005): Algorithm too slow?

More information

High Performance Computing Lecture 21. Matthew Jacob Indian Institute of Science

High Performance Computing Lecture 21. Matthew Jacob Indian Institute of Science High Performance Computing Lecture 21 Matthew Jacob Indian Institute of Science Semaphore Examples Semaphores can do more than mutex locks Example: Consider our concurrent program where process P1 reads

More information

Exercise 1.1 Hello world

Exercise 1.1 Hello world Exercise 1.1 Hello world The goal of this exercise is to verify that computer and compiler setup are functioning correctly. To verify that your setup runs fine, compile and run the hello world example

More information

CSE 374 Programming Concepts & Tools

CSE 374 Programming Concepts & Tools CSE 374 Programming Concepts & Tools Hal Perkins Fall 2017 Lecture 22 Shared-Memory Concurrency 1 Administrivia HW7 due Thursday night, 11 pm (+ late days if you still have any & want to use them) Course

More information

C09: Process Synchronization

C09: Process Synchronization CISC 7310X C09: Process Synchronization Hui Chen Department of Computer & Information Science CUNY Brooklyn College 3/29/2018 CUNY Brooklyn College 1 Outline Race condition and critical regions The bounded

More information

(5-1) Object-Oriented Programming (OOP) and C++ Instructor - Andrew S. O Fallon CptS 122 (February 4, 2019) Washington State University

(5-1) Object-Oriented Programming (OOP) and C++ Instructor - Andrew S. O Fallon CptS 122 (February 4, 2019) Washington State University (5-1) Object-Oriented Programming (OOP) and C++ Instructor - Andrew S. O Fallon CptS 122 (February 4, 2019) Washington State University Key Concepts 2 Object-Oriented Design Object-Oriented Programming

More information

Exception with arguments

Exception with arguments Exception Handling Introduction : Fundamental Syntax for Exception Handling code : try catch throw Multiple exceptions Exception with arguments Introduction Exception: An abnormal condition that arises

More information

Dealing with Issues for Interprocess Communication

Dealing with Issues for Interprocess Communication Dealing with Issues for Interprocess Communication Ref Section 2.3 Tanenbaum 7.1 Overview Processes frequently need to communicate with other processes. In a shell pipe the o/p of one process is passed

More information

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

Overview. CMSC 330: Organization of Programming Languages. Concurrency. Multiprocessors. Processes vs. Threads. Computation Abstractions CMSC 330: Organization of Programming Languages Multithreaded Programming Patterns in Java CMSC 330 2 Multiprocessors Description Multiple processing units (multiprocessor) From single microprocessor to

More information

Real Time Operating Systems and Middleware

Real Time Operating Systems and Middleware Real Time Operating Systems and Middleware POSIX Threads Synchronization Luca Abeni abeni@dit.unitn.it Real Time Operating Systems and Middleware p. 1 Threads Synchronisation All the threads running in

More information

Systèmes d Exploitation Avancés

Systèmes d Exploitation Avancés Systèmes d Exploitation Avancés Instructor: Pablo Oliveira ISTY Instructor: Pablo Oliveira (ISTY) Systèmes d Exploitation Avancés 1 / 32 Review : Thread package API tid thread create (void (*fn) (void

More information

Program threaded-fft-mutex.cc

Program threaded-fft-mutex.cc 1 // 2D FFT Using threads 2 // George F. Riley, Georgia Tech, Fall 2009 3 // This illustrates how a mutex would be implemented (both a "buggy" version 4 // and a good one). 5 6 #include 7 8

More information

ECE462Exam3. 08:10-09:20AM, November 20, 2009

ECE462Exam3. 08:10-09:20AM, November 20, 2009 ECE462Exam3 08:10-09:20AM, November 20, 2009 IcertifythatIwillnotreceivenorprovideaidtoanyotherstudentforthisexam. Signature: Youmustsignhere.Otherwise,theexamisnotgraded. This exam is printed double sides.

More information

CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages CMSC 330: Organization of Programming Languages Multithreading Multiprocessors Description Multiple processing units (multiprocessor) From single microprocessor to large compute clusters Can perform multiple

More information

Exceptions, Case Study-Exception handling in C++.

Exceptions, Case Study-Exception handling in C++. PART III: Structuring of Computations- Structuring the computation, Expressions and statements, Conditional execution and iteration, Routines, Style issues: side effects and aliasing, Exceptions, Case

More information

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst Operating Systems CMPSCI 377 Spring 2017 Mark Corner University of Massachusetts Amherst What is a Monitor? Ties data and the synchronization operations together Monitors guarantee mutual exclusion, i.e.,

More information

CSE 451: Operating Systems Winter Lecture 7 Synchronization. Steve Gribble. Synchronization. Threads cooperate in multithreaded programs

CSE 451: Operating Systems Winter Lecture 7 Synchronization. Steve Gribble. Synchronization. Threads cooperate in multithreaded programs CSE 451: Operating Systems Winter 2005 Lecture 7 Synchronization Steve Gribble Synchronization Threads cooperate in multithreaded programs to share resources, access shared data structures e.g., threads

More information

Cache Awareness. Course Level: CS1/CS2. PDC Concepts Covered: PDC Concept Locality False Sharing

Cache Awareness. Course Level: CS1/CS2. PDC Concepts Covered: PDC Concept Locality False Sharing Cache Awareness Course Level: CS1/CS PDC Concepts Covered: PDC Concept Locality False Sharing Bloom Level C C Programming Knowledge Prerequisites: Know how to compile Java/C++ Be able to understand loops

More information

EECE.4810/EECE.5730: Operating Systems Spring 2017 Homework 2 Solution

EECE.4810/EECE.5730: Operating Systems Spring 2017 Homework 2 Solution 1. (15 points) A system with two dual-core processors has four processors available for scheduling. A CPU-intensive application (e.g., a program that spends most of its time on computation, not I/O or

More information

ECE 462 Exam 3. 11:30AM-12:20PM, November 17, 2010

ECE 462 Exam 3. 11:30AM-12:20PM, November 17, 2010 ECE 462 Exam 3 11:30AM-12:20PM, November 17, 2010 I will not receive nor provide aid to any other student for this exam. Signature: You must sign here. Otherwise, the exam is not graded. This exam is printed

More information

ECE 574 Cluster Computing Lecture 8

ECE 574 Cluster Computing Lecture 8 ECE 574 Cluster Computing Lecture 8 Vince Weaver http://web.eece.maine.edu/~vweaver vincent.weaver@maine.edu 16 February 2017 Announcements Too many snow days Posted a video with HW#4 Review HW#5 will

More information

Chenyu Zheng. CSCI 5828 Spring 2010 Prof. Kenneth M. Anderson University of Colorado at Boulder

Chenyu Zheng. CSCI 5828 Spring 2010 Prof. Kenneth M. Anderson University of Colorado at Boulder Chenyu Zheng CSCI 5828 Spring 2010 Prof. Kenneth M. Anderson University of Colorado at Boulder Actuality Introduction Concurrency framework in the 2010 new C++ standard History of multi-threading in C++

More information

Distributed Real-Time Control Systems. Lecture 21 Condition Variables Typical ConcurrencyProblems

Distributed Real-Time Control Systems. Lecture 21 Condition Variables Typical ConcurrencyProblems Distributed Real-Time Control Systems Lecture 21 Condition Variables Typical ConcurrencyProblems 1 Synchronization between Tasks Consider a distributed control system where the sensor reading and control

More information

tcsc 2016 Luca Brianza 1 Luca Brianza 19/07/16 INFN & University of Milano-Bicocca

tcsc 2016 Luca Brianza 1 Luca Brianza 19/07/16 INFN & University of Milano-Bicocca tcsc 2016 1 1 INFN & University of Milano-Bicocca Outlook Amdahl s law Different ways of parallelism: - Asynchronous task execution - Threads Resource protection/thread safety - The problem - Low-level

More information

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010 CSE 374 Programming Concepts & Tools Hal Perkins Spring 2010 Lecture 19 Introduction ti to C++ C++ C++ is an enormous language: g All of C Classes and objects (kind of like Java, some crucial differences)

More information

CMSC 330: Organization of Programming Languages. Concurrency & Multiprocessing

CMSC 330: Organization of Programming Languages. Concurrency & Multiprocessing CMSC 330: Organization of Programming Languages Concurrency & Multiprocessing Multiprocessing Multiprocessing: The use of multiple parallel computations We have entered an era of multiple cores... Hyperthreading

More information

Department of Computer Science. COS 122 Operating Systems. Practical 3. Due: 22:00 PM

Department of Computer Science. COS 122 Operating Systems. Practical 3. Due: 22:00 PM Department of Computer Science COS 122 Operating Systems Practical 3 Due: 2018-09-13 @ 22:00 PM August 30, 2018 PLAGIARISM POLICY UNIVERSITY OF PRETORIA The Department of Computer Science considers plagiarism

More information

Midterm on next week Tuesday May 4. CS 361 Concurrent programming Drexel University Fall 2004 Lecture 9

Midterm on next week Tuesday May 4. CS 361 Concurrent programming Drexel University Fall 2004 Lecture 9 CS 361 Concurrent programming Drexel University Fall 2004 Lecture 9 Bruce Char and Vera Zaychik. All rights reserved by the author. Permission is given to students enrolled in CS361 Fall 2004 to reproduce

More information

std::async() in C++11 Basic Multithreading

std::async() in C++11 Basic Multithreading MÜNSTER std::async() in C++11 Basic Multithreading 2. December 2015 std::thread MÜNSTER std::async() in C++11 2 /14 #include void hello(){ std::cout

More information

Performance Throughput Utilization of system resources

Performance Throughput Utilization of system resources Concurrency 1. Why concurrent programming?... 2 2. Evolution... 2 3. Definitions... 3 4. Concurrent languages... 5 5. Problems with concurrency... 6 6. Process Interactions... 7 7. Low-level Concurrency

More information

Threaded Programming. Lecture 9: Alternatives to OpenMP

Threaded Programming. Lecture 9: Alternatives to OpenMP Threaded Programming Lecture 9: Alternatives to OpenMP What s wrong with OpenMP? OpenMP is designed for programs where you want a fixed number of threads, and you always want the threads to be consuming

More information

CSE 160 Lecture 14. Floating Point Arithmetic Condition Variables

CSE 160 Lecture 14. Floating Point Arithmetic Condition Variables CSE 16 Lecture 14 Floating Point Arithmetic Condition Variables Announcements Pick up your regrade exams in office hours today All exams will be moved to student affairs on Wednesday 213 Scott B Baden

More information

Lecture 8: September 30

Lecture 8: September 30 CMPSCI 377 Operating Systems Fall 2013 Lecture 8: September 30 Lecturer: Prashant Shenoy Scribe: Armand Halbert 8.1 Semaphores A semaphore is a more generalized form of a lock that can be used to regulate

More information

Threads Chate Patanothai

Threads Chate Patanothai Threads Chate Patanothai Objectives Knowing thread: 3W1H Create separate threads Control the execution of a thread Communicate between threads Protect shared data C. Patanothai Threads 2 What are threads?

More information

CSE 374 Programming Concepts & Tools. Hal Perkins Fall 2015 Lecture 19 Introduction to C++

CSE 374 Programming Concepts & Tools. Hal Perkins Fall 2015 Lecture 19 Introduction to C++ CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 19 Introduction to C++ C++ C++ is an enormous language: All of C Classes and objects (kind of like Java, some crucial differences) Many

More information

Java Programming Lecture 23

Java Programming Lecture 23 Java Programming Lecture 23 Alice E. Fischer April 19, 2012 Alice E. Fischer () Java Programming - L23... 1/20 April 19, 2012 1 / 20 Outline 1 Thread Concepts Definition and Purpose 2 Java Threads Creation

More information

CMSC 132: Object-Oriented Programming II. Threads in Java

CMSC 132: Object-Oriented Programming II. Threads in Java CMSC 132: Object-Oriented Programming II Threads in Java 1 Problem Multiple tasks for computer Draw & display images on screen Check keyboard & mouse input Send & receive data on network Read & write files

More information

ECE 598 Advanced Operating Systems Lecture 23

ECE 598 Advanced Operating Systems Lecture 23 ECE 598 Advanced Operating Systems Lecture 23 Vince Weaver http://web.eece.maine.edu/~vweaver vincent.weaver@maine.edu 21 April 2016 Don t forget HW#9 Midterm next Thursday Announcements 1 Process States

More information

CSE 451: Operating Systems Winter Lecture 7 Synchronization. Hank Levy 412 Sieg Hall

CSE 451: Operating Systems Winter Lecture 7 Synchronization. Hank Levy 412 Sieg Hall CSE 451: Operating Systems Winter 2003 Lecture 7 Synchronization Hank Levy Levy@cs.washington.edu 412 Sieg Hall Synchronization Threads cooperate in multithreaded programs to share resources, access shared

More information

Other consistency models

Other consistency models Last time: Symmetric multiprocessing (SMP) Lecture 25: Synchronization primitives Computer Architecture and Systems Programming (252-0061-00) CPU 0 CPU 1 CPU 2 CPU 3 Timothy Roscoe Herbstsemester 2012

More information

Synchronization COMPSCI 386

Synchronization COMPSCI 386 Synchronization COMPSCI 386 Obvious? // push an item onto the stack while (top == SIZE) ; stack[top++] = item; // pop an item off the stack while (top == 0) ; item = stack[top--]; PRODUCER CONSUMER Suppose

More information

Computer Organization and Assembly Language (CS-506)

Computer Organization and Assembly Language (CS-506) Computer Organization and Assembly Language (CS-506) Muhammad Zeeshan Haider Ali Lecturer ISP. Multan ali.zeeshan04@gmail.com https://zeeshanaliatisp.wordpress.com/ Lecture 2 Memory Organization and Structure

More information

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

INF 212 ANALYSIS OF PROG. LANGS CONCURRENCY. Instructors: Crista Lopes Copyright Instructors. INF 212 ANALYSIS OF PROG. LANGS CONCURRENCY Instructors: Crista Lopes Copyright Instructors. Basics Concurrent Programming More than one thing at a time Examples: Network server handling hundreds of clients

More information

Program threaded-fft-bakery.cc

Program threaded-fft-bakery.cc 1 // 2D FFT Using threads 2 // George F. Riley, Georgia Tech, Fall 2009 3 // This illustrates how a mutex would be implemented using Leslie Lamport s 4 // "Bakery Algorithm". This algorithm implements

More information

Project Compiler. CS031 TA Help Session November 28, 2011

Project Compiler. CS031 TA Help Session November 28, 2011 Project Compiler CS031 TA Help Session November 28, 2011 Motivation Generally, it s easier to program in higher-level languages than in assembly. Our goal is to automate the conversion from a higher-level

More information

CMSC421: Principles of Operating Systems

CMSC421: Principles of Operating Systems CMSC421: Principles of Operating Systems Nilanjan Banerjee Assistant Professor, University of Maryland Baltimore County nilanb@umbc.edu http://www.csee.umbc.edu/~nilanb/teaching/421/ Principles of Operating

More information

CS510 Advanced Topics in Concurrency. Jonathan Walpole

CS510 Advanced Topics in Concurrency. Jonathan Walpole CS510 Advanced Topics in Concurrency Jonathan Walpole Threads Cannot Be Implemented as a Library Reasoning About Programs What are the valid outcomes for this program? Is it valid for both r1 and r2 to

More information

CMSC421: Principles of Operating Systems

CMSC421: Principles of Operating Systems CMSC421: Principles of Operating Systems Nilanjan Banerjee Assistant Professor, University of Maryland Baltimore County nilanb@umbc.edu http://www.csee.umbc.edu/~nilanb/teaching/421/ Principles of Operating

More information

Consistency: Strict & Sequential. SWE 622, Spring 2017 Distributed Software Engineering

Consistency: Strict & Sequential. SWE 622, Spring 2017 Distributed Software Engineering Consistency: Strict & Sequential SWE 622, Spring 2017 Distributed Software Engineering Review: Real Architectures N-Tier Web Architectures Internet Clients External Cache Internal Cache Web Servers Misc

More information

More Functions. Pass by Value. Example: Exchange two numbers. Storage Classes. Passing Parameters by Reference. Pass by value and by reference

More Functions. Pass by Value. Example: Exchange two numbers. Storage Classes. Passing Parameters by Reference. Pass by value and by reference Pass by Value More Functions Different location in memory Changes to the parameters inside the function body have no effect outside of the function. 2 Passing Parameters by Reference Example: Exchange

More information

Multi-threaded processors. Hung-Wei Tseng x Dean Tullsen

Multi-threaded processors. Hung-Wei Tseng x Dean Tullsen Multi-threaded processors Hung-Wei Tseng x Dean Tullsen OoO SuperScalar Processor Fetch instructions in the instruction window Register renaming to eliminate false dependencies edule an instruction to

More information

Programming C++ Lecture 6. Howest, Fall 2013 Instructor: Dr. Jennifer B. Sartor

Programming C++ Lecture 6. Howest, Fall 2013 Instructor: Dr. Jennifer B. Sartor Programming C++ Lecture 6 Howest, Fall 2013 Instructor: Dr. Jennifer B. Sartor Jennifer.sartor@elis.ugent.be S Friends 2 Friends of Objects S Classes sometimes need friends. S Friends are defined outside

More information

CSCI4430 Data Communication and Computer Networks. Pthread Programming. ZHANG, Mi Jan. 26, 2017

CSCI4430 Data Communication and Computer Networks. Pthread Programming. ZHANG, Mi Jan. 26, 2017 CSCI4430 Data Communication and Computer Networks Pthread Programming ZHANG, Mi Jan. 26, 2017 Outline Introduction What is Multi-thread Programming Why to use Multi-thread Programming Basic Pthread Programming

More information

assembler Machine Code Object Files linker Executable File

assembler Machine Code Object Files linker Executable File CSCE A211 Programming Intro What is a Programming Language Assemblers, Compilers, Interpreters A compiler translates programs in high level languages into machine language that can be executed by the computer.

More information

Design Patterns in C++

Design Patterns in C++ Design Patterns in C++ Concurrency Giuseppe Lipari http://retis.sssup.it/~lipari Scuola Superiore Sant Anna Pisa May 2, 2011 G. Lipari (Scuola Superiore Sant Anna) Concurrency May 2, 2011 1 / 34 Outline

More information

5. Applicative Programming. 1. Juli 2011

5. Applicative Programming. 1. Juli 2011 1. Juli 2011 Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 1 of 41 Outline Recapitulation Computer architecture extended: Registers and caches Header files Global variables

More information

Operating Systems, Assignment 2 Threads and Synchronization

Operating Systems, Assignment 2 Threads and Synchronization Operating Systems, Assignment 2 Threads and Synchronization Responsible TA's: Zohar and Matan Assignment overview The assignment consists of the following parts: 1) Kernel-level threads package 2) Synchronization

More information

CS 153 Lab4 and 5. Kishore Kumar Pusukuri. Kishore Kumar Pusukuri CS 153 Lab4 and 5

CS 153 Lab4 and 5. Kishore Kumar Pusukuri. Kishore Kumar Pusukuri CS 153 Lab4 and 5 CS 153 Lab4 and 5 Kishore Kumar Pusukuri Outline Introduction A thread is a straightforward concept : a single sequential flow of control. In traditional operating systems, each process has an address

More information

Lecture 10 Midterm review

Lecture 10 Midterm review Lecture 10 Midterm review Announcements The midterm is on Tue Feb 9 th in class 4Bring photo ID 4You may bring a single sheet of notebook sized paper 8x10 inches with notes on both sides (A4 OK) 4You may

More information

CS-537: Midterm Exam (Spring 2001)

CS-537: Midterm Exam (Spring 2001) CS-537: Midterm Exam (Spring 2001) Please Read All Questions Carefully! There are seven (7) total numbered pages Name: 1 Grading Page Points Total Possible Part I: Short Answers (12 5) 60 Part II: Long

More information

Computation Abstractions. Processes vs. Threads. So, What Is a Thread? CMSC 433 Programming Language Technologies and Paradigms Spring 2007

Computation Abstractions. Processes vs. Threads. So, What Is a Thread? CMSC 433 Programming Language Technologies and Paradigms Spring 2007 CMSC 433 Programming Language Technologies and Paradigms Spring 2007 Threads and Synchronization May 8, 2007 Computation Abstractions t1 t1 t4 t2 t1 t2 t5 t3 p1 p2 p3 p4 CPU 1 CPU 2 A computer Processes

More information

CSE 160 Lecture 7. C++11 threads C++11 memory model

CSE 160 Lecture 7. C++11 threads C++11 memory model CSE 160 Lecture 7 C++11 threads C++11 memory model Today s lecture C++ threads The C++11 Memory model 2013 Scott B. Baden / CSE 160 / Winter 2013 2 C++11 Threads Via , C++ supports a threading

More information

Discussion 1H Notes (Week 3, April 14) TA: Brian Choi Section Webpage:

Discussion 1H Notes (Week 3, April 14) TA: Brian Choi Section Webpage: Discussion 1H Notes (Week 3, April 14) TA: Brian Choi (schoi@cs.ucla.edu) Section Webpage: http://www.cs.ucla.edu/~schoi/cs31 More on Arithmetic Expressions The following two are equivalent:! x = x + 5;

More information

Introduction to Java Threads

Introduction to Java Threads Object-Oriented Programming Introduction to Java Threads RIT CS 1 "Concurrent" Execution Here s what could happen when you run this Java program and launch 3 instances on a single CPU architecture. The

More information

COMP 346 WINTER Tutorial 2 SHARED DATA MANIPULATION AND SYNCHRONIZATION

COMP 346 WINTER Tutorial 2 SHARED DATA MANIPULATION AND SYNCHRONIZATION COMP 346 WINTER 2018 1 Tutorial 2 SHARED DATA MANIPULATION AND SYNCHRONIZATION REVIEW - MULTITHREADING MODELS 2 Some operating system provide a combined user level thread and Kernel level thread facility.

More information

Intermediate Programming, Spring 2017*

Intermediate Programming, Spring 2017* 600.120 Intermediate Programming, Spring 2017* Misha Kazhdan *Much of the code in these examples is not commented because it would otherwise not fit on the slides. This is bad coding practice in general

More information

COMP6771 Advanced C++ Programming

COMP6771 Advanced C++ Programming 1. COMP6771 Advanced C++ Programming Week 10 Multithreading - Producer/Consumer Problem 2016 www.cse.unsw.edu.au/ cs6771 2. : C++11 Mutexes C++11 provides Mutex objects in the header file. General

More information

Module 7 b. -Namespaces -Exceptions handling

Module 7 b. -Namespaces -Exceptions handling Module 7 b -Namespaces -Exceptions handling C++ Namespace Often, a solution to a problem will have groups of related classes and other declarations, such as functions, types, and constants. C++provides

More information

Introduction to C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts

Introduction to C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts Introduction to C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2 nd edition, by Kernighan and Ritchie, Absolute C++, by Walter

More information

Outline of lecture. i219 Software Design Methodology 10. Multithreaded programming. Kazuhiro Ogata (JAIST)

Outline of lecture. i219 Software Design Methodology 10. Multithreaded programming. Kazuhiro Ogata (JAIST) i219 Software Design Methodology 10. Multithreaded programming Kazuhiro Ogata (JAIST) Outline of lecture 2 Thread Race condition Synchronization Deadlock Bounded buffer problem Thread (1) 3 Units of execution.

More information

Allows program to be incrementally parallelized

Allows program to be incrementally parallelized Basic OpenMP What is OpenMP An open standard for shared memory programming in C/C+ + and Fortran supported by Intel, Gnu, Microsoft, Apple, IBM, HP and others Compiler directives and library support OpenMP

More information

A brief introduction to C++

A brief introduction to C++ A brief introduction to C++ Rupert Nash r.nash@epcc.ed.ac.uk 13 June 2018 1 References Bjarne Stroustrup, Programming: Principles and Practice Using C++ (2nd Ed.). Assumes very little but it s long Bjarne

More information

Datenstrukturen und Algorithmen

Datenstrukturen und Algorithmen 1 Datenstrukturen und Algorithmen Exercise 12 FS 2018 Program of today 2 1 Feedback of last exercise 2 Repetition theory 3 Programming Tasks 1. Feedback of last exercise 3 Football Championship 4 Club

More information

Functions and Recursion

Functions and Recursion Functions and Recursion 1 Storage Classes Scope Rules Functions with Empty Parameter Lists Inline Functions References and Reference Parameters Default Arguments Unary Scope Resolution Operator Function

More information

Your first C and C++ programs

Your first C and C++ programs Your first C and C++ programs Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2 nd edition, by Kernighan and Ritchie, Absolute C++,

More information

C++ Programming Lecture 11 Software Engineering Group

C++ Programming Lecture 11 Software Engineering Group C++ Programming Lecture 11 Software Engineering Group Philipp D. Schubert Contents 1. High performance computing 2. High performance computing in C++ 1. std::thread 2. std::future 3. std::async 4. std::future

More information

TCSS 422: OPERATING SYSTEMS

TCSS 422: OPERATING SYSTEMS TCSS 422: OPERATING SYSTEMS OBJECTIVES Introduction to threads Concurrency: An Introduction Wes J. Lloyd Institute of Technology University of Washington - Tacoma Race condition Critical section Thread

More information

Week 0: Intro to Computers and Programming. 1.1 Why Program? 1.2 Computer Systems: Hardware and Software. Hardware Components

Week 0: Intro to Computers and Programming. 1.1 Why Program? 1.2 Computer Systems: Hardware and Software. Hardware Components Week 0: Intro to Computers and Programming Gaddis: Sections 1.1-3 and 2.1 CS 1428 Fall 2014 Jill Seaman 1.1 Why Program? Computer programmable machine designed to follow instructions Program instructions

More information

Synchronization. CS61, Lecture 18. Prof. Stephen Chong November 3, 2011

Synchronization. CS61, Lecture 18. Prof. Stephen Chong November 3, 2011 Synchronization CS61, Lecture 18 Prof. Stephen Chong November 3, 2011 Announcements Assignment 5 Tell us your group by Sunday Nov 6 Due Thursday Nov 17 Talks of interest in next two days Towards Predictable,

More information

CSE 333 Section 9 - pthreads

CSE 333 Section 9 - pthreads CSE 333 Section 9 - pthreads Welcome back to section! We re glad that you re here :) Process A process has a virtual address space. Each process is started with a single thread, but can create additional

More information

CSE 303: Concepts and Tools for Software Development

CSE 303: Concepts and Tools for Software Development CSE 303: Concepts and Tools for Software Development Hal Perkins Autumn 2008 Lecture 24 Introduction to C++ CSE303 Autumn 2008, Lecture 24 1 C++ C++ is an enormous language: All of C Classes and objects

More information

CS 11 C++ track: lecture 1

CS 11 C++ track: lecture 1 CS 11 C++ track: lecture 1 Administrivia Need a CS cluster account http://www.cs.caltech.edu/cgi-bin/ sysadmin/account_request.cgi Need to know UNIX (Linux) www.its.caltech.edu/its/facilities/labsclusters/

More information

Assembly Language. Lecture 2 - x86 Processor Architecture. Ahmed Sallam

Assembly Language. Lecture 2 - x86 Processor Architecture. Ahmed Sallam Assembly Language Lecture 2 - x86 Processor Architecture Ahmed Sallam Introduction to the course Outcomes of Lecture 1 Always check the course website Don t forget the deadline rule!! Motivations for studying

More information