Real-Time Software (RTSOF) EVMINX9 Week 2

Vergelijkbare documenten
Inhoud RTSYST. Week 1 Week 2 Week 3 Week 4 Week 5 Week 6 Week 7

Real-Time Systems (RTSYST)

Programmeertechnieken Week 6

Tentamen Imperatief Programmeren

Kleine cursus PHP5. Auteur: Raymond Moesker

Modelleren en Programmeren

Universiteit van Amsterdam FNWI. Voorbeeld van tussentoets Inleiding programmeren

Advanced C ++ Programming Datastructuren 2018

Datastructuren Werkcollege Intro

Datastructuren: stapels, rijen en binaire bomen

Concurrency in Java met threads. Java Threads. Voorbeelden concurrency in applicaties. Waarom concurrency in Java?

Inleiding Software Engineering! Unit Testing, Contracten, Debugger! 13 Februari 2014!

ASP.NET Test Jan Van Ryckeghem

Tentamen Object Georiënteerd Programmeren TI januari 2013, Afdeling SCT, Faculteit EWI, TU Delft

Uitwerkingen Tweede deeltentamen Imperatief programmeren Vrijdag 15 oktober 2010, uur

Uitwerking Tentamen Modelleren en Programmeren - versie 1 Woensdag 1 februari 2017, uur

Hoofdstuk 1: Inleiding. Hoofdstuk 2: Klassen en objecten Datahiding: afschermen van implementatiedetails. Naar de buitenwereld toe enkel interfaces.

Uitwerking Tentamen Modelleren en Programmeren - versie 1 Woensdag 1 februari 2017, uur

int getaantalpassagiers{): void setaantalpassagiers(int aantalpassagiers);

NAAM: Programmeren 1 Examen 21/01/2011

Datastructuren: stapels, rijen en binaire bomen

Vakgroep CW KAHO Sint-Lieven

Tentamen Object Georiënteerd Programmeren TI oktober 2014, Afdeling SCT, Faculteit EWI, TU Delft

HOGESCHOOL VAN AMSTERDAM Informatica Opleiding. CPP 1 van 10


Uitwerking Tweede deeltentamen Imperatief programmeren - versie 1 Vrijdag 21 oktober 2016, uur

Modelleren en Programmeren

Tentamen Objectgeorienteerd Programmeren IN1205 Voorbeeld

Tentamen Computersystemen

Modeleren. Modelleren. Together UML. Waarvan maken we een model? overzicht les 14 t/m 18. ControlCenter 6.2

Programmeren 1 23 januari 2013 Prof. T. Schrijvers

C# 6 Door Alex en Chris van Beek

Objectgericht Programmeren. (in Python)

Datatypes Een datatype is de sort van van een waarde van een variabele, veel gebruikte datatypes zijn: String, int, Bool, char en double.

Programmeermethoden. Recursie. week 11: november kosterswa/pm/

Overerving & Polymorfisme

Zelftest Programmeren in Java

Programmeren in Java 3

Modulewijzer Tirdat01

Inleiding C++ Coding Conventions

Wie ben ik? Agile Software Development. Het waterval model. Inhoud

Bescherming van (software) IP bij uitbesteding van productie

Inleiding Een terugblik op C... 3

Abstracte klassen & Interfaces

Kwis (3) class X { public void a() { System.out.println("x"); public static void main(string[] args) { X x = new X();

Game of Life in Java

Programmeermethoden. Functies vervolg. Walter Kosters. week 5: 1 5 oktober kosterswa/pm/

Ingebouwde klassen & methodes

Dynamisch Programmeren. Het Rugzakprobleem

Objectgeoriënteerd Programmeren in C++

Derde deeltentamen Imperatief programmeren - versie 1 Vrijdag 9 november 2018, uur

N&O: Objectgericht Programmeren. (in Python)

Programmeermethoden. Pointers. Walter Kosters. week 10: november kosterswa/pm/

Voorbeeldtentamen Inleiding programmeren (IN1608WI), Oktober 2003, , Technische Universiteit Delft, Faculteit EWI, Afdeling 2.

Programmeermethoden. Recursie. Walter Kosters. week 11: november kosterswa/pm/

Een unit test is geen integratie test. Niet het hele systeem, maar onderdelen van een systeem worden getest.

NAAM: Programmeren 1 Examen 29/08/2012

Scala. Korte introductie. Sylvia Stuurman

Elementary Data Structures 3

IMP Uitwerking week 13

NSPYRE LEGO MINDSTORMS UITDAGING (JAVA) INLEIDING. DOEL: SIMULATOR:

Syntax van opdracht. opdracht. expressie. variabele. = expressie ; klasse naam. methode naam. property naam += object

Threading in.net (C#)

In BlueJ. Doe onderstaande met muis/menu s:

Vraag 1: Software Levenscyclus

Voorbeeld: Simulatie van bewegende deeltjes

Lessen Java: Reeks 3. David Blinder Jan G. Cornelis

NHibernate als ORM oplossing

Windows-applicatie. using System.Windows.Forms; using System.Drawing; class HalloWin1 { static void Main ( )

Verslag Opdracht 4: Magische Vierkanten

Uitwerkingen derde deeltentamen Gameprogrammeren Vrijdag 6 november 2015, uur

Centrale begrippen hoofdstuk 3. Waarom multiprogramming? Vandaag. processen proces state: running, ready, blocked,... Vragen??

Deel 8: stappenmotoren en interrupts

Deel 1 : Gesloten Boek

Transcriptie:

Real-Time Software (RTSOF) EVMINX9 Week 2

C++ Threads C++ heeft (nog) geen standaard library voor concurrent programmeren. Boost Thread library http://www.boost.org/ Intel Threading Building Blocks (TBB) http://www.threadingbuildingblocks.org/ Parallel Pattern Library (PPL) http://msdn.microsoft.com/en-us/magazine/dd434652.aspx Er zijn ook uitbreidingen van C++ die concurrency aan de taal toevoegen (met taalconstructies). µc++ voegt onder andere taalconstructies task en monitor toe. http://plg.uwaterloo.ca/~usystem/uc++.html Real-Time? 35

Boost Thread #include <boost/thread.hpp> // void () { for (int i(0); i<10; ++i) { boost::this_thread::sleep(boost::posix_time::milliseconds(10)); std::cout<<""<<std::endl; void () { for (int i(0); i<10; ++i) { boost::this_thread::sleep(boost::posix_time::milliseconds(20)); std::cout<<""<<std::endl; int main() { boost::thread t1(&), t2(&); t1.join(); t2.join(); std::cin.get(); return 0; 36

Boost Thread #include <boost/thread.hpp> // void print(int d, const std::string& m) { for (int i(0); i<10; ++i) { boost::this_thread::sleep(boost::posix_time::milliseconds(d)); std::cout<<m<<std::endl; int main() { boost::thread t1(&print, 10, ""); boost::thread t2(&print, 20, ""); t1.join(); t2.join(); std::cin.get(); return 0; Wel type-safe (in tegenstelling tot pthreads). 37

Boost Thread thread::hardware_concurrency() Geeft het aantal hardware threads wat op het huidige systeem beschikbaar is (= aantal CPU s of cores of hyperthreading units), of 0 als deze informatie niet beschikbaar is. t.native_handle(); Geeft een instantie van native_handle_type wat gebruikt kan worden met de platform-specific API om de onderliggende implementatie te programmeren. Kan in een real-time pthread omgeving gebruikt worden om de prioriteit in te stellen. 38

Boost Synchronization Mutex mutex void lock(); bool try_lock(); void unlock(); recursive_mutex Idem maar telt aantal locks (en aantal unlocks). shared_mutex (Multiple-reader / single-writer) void lock(); bool try_lock(); void unlock(); void lock_shared(); bool try_lock_shared(); bool unlock_shared(); Lock Conditional variable Barrier 39

Mutex Voorbeeld class Point { public: Point(): x(0), y(0) { void stepnortheast() { x++; y++; bool isnortheast() { return x==y; private: int x, y; ; Wat gebeurt er als deze class in een multi-threaded omgeving gebruikt wordt? y x==y x 40

Mutex Voorbeeld class TestPoint { private: Point p; void thread1() { for (int i(0); i < 20000000; i++) p.stepnortheast(); void thread2() { for (int i(0); i < 20000000; i++) if (!p.isnortheast()) std::cout<<"probleem!"<<std::endl; public: ; Probleem! Probleem! Einde. void test() { boost::thread t1(&testpoint::thread1, this); boost::thread t2(&testpoint::thread2, this); t1.join(); t2.join(); std::cout<<"einde."<<std::endl; 41

Mutex Voorbeeld class Point { public: Point(): x(0), y(0) { void stepnortheast() { m.lock(); x++; y++; m.unlock(); bool isnortheast() { m.lock(); bool res=x==y; m.unlock(); return res; private: boost::mutex m; int x, y; ; Zonder mutex Met mutex Einde. Run-time 1.5 s 101.2 s 42

Mutex voorbeeld class Point { public: Point(): x(0), y(0) { void stepnortheast() { m.lock(); x++; y++; m.unlock(); bool isnortheast() { m.lock(); bool r(x==y); m.unlock(); return r; bool iswithinlimits() { m.lock(); bool r(x>=0&&x<=1000000&&y>=0&&y<=1000000); m.unlock(); return r; private: boost::mutex m; int x, y; ; Je wilt niet onnodig wachten! 43

Mutex voorbeeld class Point { isnortheast() en public: iswithinlimits kunnen Point(): x(0), y(0) { void stepnortheast() { nu parallel draaien! m.lock(); x++; y++; m.unlock(); bool isnortheast() { m.lock_shared(); bool r(x==y); m.unlock_shared(); return r; bool iswithinlimits() { m.lock_shared(); bool r(x>=0&&x<=1000000&&y>=0&&y<=1000000); m.unlock_shared(); return r; private: boost::shared_mutex m; int x, y; ; 44

Boost Synchronization Mutex Lock lock_guard Gebruikt RAII (Resource Acquisition Is Initialization): Lock mutex in constructor, unlock mutex in destructor unique_lock Idem als lock_guard + lock(), try_lock() en unlock() memberfuncties shared_lock Idem als unique_lock maar gebruikt _shared versies van lock, try_lock en unlock. Conditional variable Barrier Een lock maakt het gebruik van een mutex eenvoudiger en exception safe. Zie C++ exceptions (OGOPRG dictaat par.6.6). 45

Mutex Voorbeeld class Point { public: Point(): x(0), y(0) { void stepnortheast() { boost::lock_guard<boost::mutex> lock(m); x++; y++; bool isnortheast() { boost::lock_guard<boost::mutex> lock(m); return x==y; private: boost::mutex m; int x, y; ; 46

Mutex voorbeeld std::vector<int> v; boost::mutex m; m.lock(); v.at(10)=27; m.unlock(); m.lock(); try() { v.at(10)=27; m.unlock(); catch( ) { m.unlock(); throw; { Niet handig! boost::lock_guard<boost::mutex> l(m); v.at(10)=27; Oplossing?! Wat gaat er mis als at een exception gooit? 47

Boost Synchronization Mutex Lock Conditional variable conditional_variable void notify_one(); void notify_all(); void wait(boost::unique_lock<boost::mutex>& lock); void wait(boost::unique_lock<boost::mutex>& lock, pred_type pred); conditional_variable_any Barrier Idem maar werkt met elk type lock 48

Monitor voorbeeld #include <boost/thread.hpp> using namespace boost; int ei_teller(0); mutex m; condition_variable c; // consumer thread... { unique_lock<mutex> u(m); while (ei_teller<12) c.wait(u); ei_teller-=12;... // producer thread... { lock_guard<mutex> g(m); ei_teller+=n; c.notify_one();... 49

Monitor voorbeeld while (ei_teller<12) c.wait(u); // alternatief met predicate functie: bool pred() { return ei_teller>=12; c.wait(u, &pred); // alternatief met lambda functie (C++0x): c.wait(u, []() { return ei_teller>=12 ); lambda functies zijn behandeld bij de cursus ALDAT 50

Boost Synchronization Mutex Lock Conditional variable Barrier barrier barrier(unsigned count); bool wait(); Een barrier is een ontmoetingspunt (rendezvous). Pas nadat count threads een wait hebben gedaan mogen ze samen verder. 51