Advanced C++ Programming

Slides:



Advertisements
Similar presentations
Async Programming WITH ASYNC TASK
Advertisements

Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
1 ES 314 Advanced Programming Lec 3 Sept 8 Goals: complete discussion of pointers discuss 1-d array examples Selection sorting Insertion sorting 2-d arrays.
Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding.
C++ Functions. 2 Agenda What is a function? What is a function? Types of C++ functions: Types of C++ functions: Standard functions Standard functions.
Chapter 6: Functions.
Methods Review. 2. Class-wide vs. local variables. 3. Why C# bans global variables. 4. Nested blocks. 5. Scope of identifiers.
Variables, Functions & Parameter Passing CSci 588 Fall 2013 All material not from online sources copyright © Travis Desell, 2011.
CS 114 – Class 02 Topics  Computer programs  Using the compiler Assignments  Read pages for Thursday.  We will go to the lab on Thursday.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 6 Functions.
CSE 332: C++ Type Programming: Associated Types, Typedefs and Traits A General Look at Type Programming in C++ Associated types (the idea) –Let you associate.
Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1.
Pointers OVERVIEW.
C++ History C++ was designed at AT&T Bell Labs by Bjarne Stroustrup in the early 80's Based on the ‘C’ programming language C++ language standardised in.
1 Overloading Overloading allows a function or operator to have a different meaning depending on the type of objects it is used on. Examples: operator+
C++ Functions. Objectives 1. Be able to implement C++ functions 2. Be able to share data among functions 2.
Nachos Project 4 Lecturer: Hao-Hua Chu TA: Chun-Po Wang (Artoo) Date: 2008/10/25.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 7 Clicker Questions September 22, 2009.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 5 Looping.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
Structure Programming Lecture 8 Chapter 5&6 - Function – part I 12 December 2015.
1 Brief Version of Starting Out with C++, 4th Brief Edition Chapter 6 Functions.
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
1 Standard Version of Starting Out with C++, 4th Brief Edition Chapter 5 Looping.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assignment 4 is due Nov. 20 (next Friday). After today you should know everything you need for assignment.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 6 Functions.
Functions Chapter 6. Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules Function: a collection.
Lecture 5 functions 1 © by Pearson Education, Inc. All Rights Reserved.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Chapter 1 C++ Basics Review (Section 1.4). Classes Defines the organization of a data user-defined type. Members can be  Data  Functions/Methods Information.
1 Introduction to Object Oriented Programming Chapter 10.
CMSC 202 Computer Science II for Majors. CMSC 202UMBC Topics Exceptions Exception handling.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures C++ Review 2.
Functions in C++ Top Down Design with Functions. Top-down Design Big picture first broken down into smaller pieces.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Building Programs from Existing Information Solutions for programs often can be developed from previously solved problems. Data requirements and solution.
FUNCTIONS (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
1 Chapter 8 Scope, Lifetime, and More on Functions CS185/09 - Introduction to Programming Caldwell College.
Software Engineering Algorithms, Compilers, & Lifecycle.
C++ REVIEW – TEMPLATES. GENERIC PROGRAMMING Programming/developing algorithms with the abstraction of types Algorithms/data is expressed “without type”
CSE 332: C++ Exceptions Motivation for C++ Exceptions Void Number:: operator/= (const double denom) { if (denom == 0.0) { // what to do here? } m_value.
1 Project 12: Cars from File. This is an extension of Project 11, Car Class You may use the posted solution for Project 11 as a starting point for this.
“Generic Programming” ECE 297
Pointers and Dynamic Arrays
COMP 170 – Introduction to Object Oriented Programming
C++ History C++ was designed at AT&T Bell Labs by Bjarne Stroustrup in the early 80's Based on the ‘C’ programming language C++ language standardised in.
Chapter 5: Looping Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
This pointer, Dynamic memory allocation, Constructors and Destructor
Templates in C++.
group work #hifiTeam
Templates ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED BY NANCY M. AMATO AND JORY DENNY.
ICS Principles of Operating Systems
Vectors.
Chapter 6: Functions Starting Out with C++ Early Objects Ninth Edition
Functions A function is a “pre-packaged” block of code written to perform a well-defined task Why? Code sharing and reusability Reduces errors Write and.
Functions How to reuse code.
7. 11 Introduction to C++ Standard Library Class Template vector (Cont
Constructors and Other Tools
Exception Handling.
7 Arrays.
Functions continued.
COP 3330 Object-oriented Programming in C++
Recitation Course 0603 Speaker: Liu Yu-Jiun.
Pointers and dynamic objects
ENERGY 211 / CME 211 Lecture 8 October 8, 2008.
ASEE / GradSWE C++ Workshop
Programming Concepts and Database
Standard Version of Starting Out with C++, 4th Edition
Presentation transcript:

Advanced C++ Programming Assignment 3 2016/2017 Přemysl Čech

Async tasks – continue with Goal is to: implement the class task with functions implement global functions operating with task(s) Task represents an async call of a function on a new thread the motivation: we want to chain task(s) together the function „continue_with“ we want to comfortably control multiple running task(s)

Async tasks – continue with template<class Ret> class task the template class – one parameter – a return type task(… f) (a constructor) - obtains a function f that will be called asynchronously on demand an evaluation doesn‘t start immediately! the function f is parameterless the return value of f has to match the return type of the class task functions start() – starts an async evaluation of the passed function f get_result() – returns a result of the function f (a blocking call) other functions that are not mandatory but might be useful has_started(), is_running(), …

Async tasks – continue with template<class NRet> task<NRet> task<Ret>::continue_with(… g) obtains a new following function g that will be called after the original task is finished is also generic (templated) because the new function g can return a different value the continue_with(…) function returns a new task with the appropriate return value the input parameter of the function g has the same type as the result of the precedent (current) task (task(s) chaining) g obtains the result from the precedent task as the input value

Async tasks – continue with Examples auto task1 = task<int>([]() { return 5; }).continue_with<int>([](int val) return val * 3; }).continue_with<void>([](int val) cout << "The result is: " << val << endl; }); //if not started, task starts task1.get_result(); //15 task<void> task1([]() { this_thread::sleep_for(chrono::milliseconds(2000)); cout << "Hello world!"; }); task1.start(); //wait for finish task1.get_result();

Async tasks – continue with Examples int x = 0; auto t1 = task<int>([&]() { return x + 5; }); auto t2 = task<void>([&]() { cout << t1.get_result() << endl; }); auto t3 = task<void>([&]() { x = 10; }); t3.get_result(); t2.get_result(); //15 t2.get_result(); //output? Second output returns nothing – it does not call function again Functions can have captures assume a reasonable usage of all task(s)

Tasks – global functions template<class T> void wait_all(vector<task<T>>& tasks) you don‘t have to keep this signature precisely the function obtains a vector (or a list or an array) of task(s) of the same return value and waits (a blocking call) until all task(s) are finished template<class T> size_t wait_any(vector<task<T>>& tasks) waits and returns the index of the first finished task

Tasks – global functions template <class T> task<T> start_new(… f) creates a new task and starts it immediately template<class T> task<T> when_any(vector<task<T>>& tasks) obtains a vector of task(s) of the same return value returns a task with the value of the first finished task auto task1 = start_new<void>([]() { cout << "Hi!" << endl; });

Tasks – global functions template<class T> task<vector<T>> when_all(vector<task<T>>& tasks) obtains a vector (or a list or an array) of task(s) of the same return value returns a task with results of all input task(s) (stored in a vector for example) an execution of all task(s) returned from functions does not start immediately (except start_new) they start after a start() or get_result() call

Tasks – global functions when_all, when_any, wait_all, wait_any all functions start the passed task(s) (if not stared before) in „when“ case lazily! if some passed task(s) have already finished before calling a wait_any or when_any function an arbitrary task is returned first

Tasks – global functions Examples auto tasks = vector<task<bool>> { start_new<bool>([]() { return is_prime(5478543); }), start_new<bool>([]() { return is_prime(1235484); }), start_new<bool>([]() { return is_prime(1201214); }) }; when_any(tasks).continue_with<void>([](bool val) { cout << "Computation of the first task has finished"; }).get_result(); when_all(tasks).continue_with<void>([](vector<bool>&& val) { cout << "Computation of all tasks have finished"; }).get_result(); wait_all(tasks); //just a check – should not block

Tasks and functions What to look for void return type – is it a problem? probably you have to write some specializations passing and returning of all parameters l-value vs. r-value copying of task(s) – should be allowed the same running copied task should be evaluated only once all get_result() calls on the same copied task should return the same reference to the return value (the function is not executed again!) if the return value cannot be returned as a reference you don‘t have to check it (it‘s a users fault)

Tasks and functions Hints look at std::thread and std::future classes they start immediately different forms of initialization (e.g. std::async) can be shared? you can use std::function for storing functions look at template specializations template<class T> class A { … }; template<> class A<int> { … };

Code and submissions You should follow best C++ practices a proper naming, no duplicate code an effective parameters passing a correct allocation and deletion of all variables a reasonable distribution of your code into functions and classes also move an implementation code to the implementation file (.cpp) if possible Submit your final solution to the information system (SIS) two files – a .cpp and .hpp files write your name in a comment at the beginning of each file (//Jarda Vomacka)