C++11 Threading Lieven de Cock

Slides:



Advertisements
Similar presentations
Threads Relation to processes Threads exist as subsets of processes Threads share memory and state information within a process Switching between threads.
Advertisements

CS Lecture 4 Programming with Posix Threads and Java Threads George Mason University Fall 2009.
E81 CSE 532S: Advanced Multi-Paradigm Software Development Chris Gill Department of Computer Science and Engineering Washington University in St. Louis.
POSIX threads and C++ facilities Jakub Yaghob. Low-level threading and synchronization support Pthreads POSIX threads IEEE POSIX c (1995) C library.
Concurrency (3) CSE 132. iClicker/WUTexter Question The following four numbers are in 8-bit 2’s complement form: Which.
Threading Part 4 CS221 – 4/27/09. The Final Date: 5/7 Time: 6pm Duration: 1hr 50mins Location: EPS 103 Bring: 1 sheet of paper, filled both sides with.
Threading Part 3 CS221 – 4/24/09. Teacher Survey Fill out the survey in next week’s lab You will be asked to assess: – The Course – The Teacher – The.
1 CS318 Project #3 Preemptive Kernel. 2 Continuing from Project 2 Project 2 involved: Context Switch Stack Manipulation Saving State Moving between threads,
Threads© Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer Science Department.
1 Advanced Computer Programming Concurrency Multithreaded Programs Copyright © Texas Education Agency, 2013.
Object Oriented Analysis & Design SDL Threads. Contents 2  Processes  Thread Concepts  Creating threads  Critical sections  Synchronizing threads.
CGS 3763 Operating Systems Concepts Spring 2013 Dan C. Marinescu Office: HEC 304 Office hours: M-Wd 11: :30 AM.
CSE 425: Concurrency III Monitors A monitor is a higher level construct for synchronizing multiple threads’ access to a common code segment –Can implement.
Multithreading in Java Project of COCS 513 By Wei Li December, 2000.
CDP 2012 Based on “C++ Concurrency In Action” by Anthony Williams and The C++11 Memory Model and GCC WikiThe C++11 Memory Model and GCC Created by Eran.
The University of Adelaide, School of Computer Science
Thread-Safe Programming Living With Linux. Thread-Safe Programming Tommy Reynolds Fedora Documentation Project Steering Committee
1 Threads  Sequential Execution: Here statements are executed one after the other.They consider only a single thread of execution, where thread is an.
Threads and Thread Control Thread Concepts Pthread Creation and Termination Pthread synchronization Threads and Signals.
CS345 Operating Systems Threads Assignment 3. Process vs. Thread process: an address space with 1 or more threads executing within that address space,
POSIX Threads Programming Operating Systems. Processes and Threads In shared memory multiprocessor architectures, such as SMPs, threads can be used to.
Synchronized and Monitors. synchronized is a Java keyword to denote a block of code which must be executed atomically (uninterrupted). It can be applied.
Threaded Programming in Python Adapted from Fundamentals of Python: From First Programs Through Data Structures CPE 401 / 601 Computer Network Systems.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Chapter 4: Threads Overview Multithreading.
Concurrency in Java Brad Vander Zanden. Processes and Threads Process: A self-contained execution environment Thread: Exists within a process and shares.
Professor: Shu-Ching Chen TA: Samira Pouyanfar.  An independent stream of instructions that can be scheduled to run  A path of execution int a, b; int.
1 Pthread Programming CIS450 Winter 2003 Professor Jinhua Guo.
IT 325 Operating systems Chapter6.  Threads can greatly simplify writing elegant and efficient programs.  However, there are problems when multiple.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Chapter 4: Threads Overview Multithreading.
Threads in Java1 Concurrency Synchronizing threads, thread pools, etc.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Concurrency & Dynamic Programming.
Multi-Threading in Java
CS 360 pthreads Condition Variables for threads. Page 2 CS 360, WSU Vancouver What is the issue? Creating a thread to perform a task and then joining.
NCHU System & Network Lab Lab #6 Thread Management Operating System Lab.
E81 CSE 532S: Advanced Multi-Paradigm Software Development Chris Gill Department of Computer Science and Engineering Washington University in St. Louis.
Thread Basic Thread operations include thread creation, termination, synchronization, data management Threads in the same process share:  Process address.
PRINCIPLES OF OPERATING SYSTEMS Tutorial-4: Multi-process and Multi-threaded Programming CPSC 457, Spring 2015 May 28/29, 2015 Department of Computer Science,
Bartosz Milewski. Cilk (MIT, Cilk Arts, Intel Cilk+) Parallel Haskell Microsoft UMS Threads (User-Mode Scheduling) (Only 64-bit and Server) Microsoft.
Concurrent Programming in Java Based on Notes by J. Johns (based on Java in a Nutshell, Learning Java) Also Java Tutorial, Concurrent Programming in Java.
CSE 532: Course Review CSE 532 Fall 2015 Final Exam 120 minutes, covering material throughout semester –1pm to 3pm on Wednesday December 16, 2015 –Arrive.
C++ - parallelization and synchronization
With C++ and some C# code samples
Prepared by Oussama Jebbar
Introduction to Concurrency: Synchronization Mechanisms
Multithreading / Concurrency
Chapter 4: Threads.
Multi-threading Streaming
Windows and C++11 Threads and Locks
A Scheme concurrency library
Slide design: Dr. Mark L. Hornick
Advanced C++ Programming
Threads Threads.
Boost String API & Threads
Thread Programming.
Multithreading Tutorial
Principles of Operating Systems Lecture 8
Lecture 13: Producer-Consumer and Semaphores
Multithreading.
PTHREADS AND SEMAPHORES
Multithreading Tutorial
Thread Programming.
Monitor Object Pattern
Synchronized Threads and Threads Coordination
Multithreading Tutorial
Multithreading Tutorial
Lecture 13: Producer-Consumer and Semaphores
Computer Science 2 06A-Java Multithreading
CMSC 202 Threads.
POSIX Threads(pthreads)
More concurrency issues
Presentation transcript:

C++11 Threading Lieven de Cock lieven.de.cock@telenet.be www.codeblocks.org

others pthread boost::thread Poco::thread Win thread … And now : std::thread

Callable Objects function , function pointers, function references object implicitly convertible to function (-/pointers/references) function objects lambdas member function pointers These things can be run in a different thread

Running a callable in a thread Pass it to the std::thread constructor Wait till finished (join) From now on the std::thread object can be destroyed Detached threads : no need to wait, thread object can be destroyed earlier Can ask if joinable Get its ID Get the native handle

Running a callable in an async Pass it to the std::async constructor Wait till finished ==> wait on its future Launch policies : - std::launch::async - std::launch::deferred - std::launch::async | std::launch::deferred - unspecified

Running a callable in a packaged task Pass it to the std::packaged_task constructor get_future Invoke now/later by it's operator() Wait till finished ==> wait on its future

sharing Mutex + std::mutex + std::recursive_mutex + std::timed_mutex + std::recursive_timed_mutex Lock guards + std::lock_guard + std::unique_lock adopt_lock / deferred_lock

synchronization Condition variable + std::condition_variable (std::mutex) + std::condition_variable_any (mutex alike) + notify_one + notify_all + wait (with time outs) + spurious wake-ups !!! → use conditions

synchronization Future (promise) + std::future (only one referring to the event/result) + std::shared_future (multiple referring to the same event/result) + becomes ready + wait (with timeouts) + get (blocking) → result or exception

atomics Native types ==> have an alias - std::atomic<bool> - std::atomic_bool Your own types Load/store/exchange/clear/test_and_set/... NO default initialization (even when not as member in constructor init list) !!! lock_free … and much more advanced stuff

futures Templatized on the type of the return value (the result) of the task Result can be accessed only once get() : blocks wait() : blocks wait_until : timepoint wait_for : duration When task launched deferred : wait_for/until will not start it, get/wait will start it shared_future : several threads can wait/get on it, result can be accessed multiple times

promises Templatized on the type of the return value (the result) set_value set_exception set_value_at_thread_exit set_exception_at_thread_exit get_future

problems _GLIBCXX_USE_NANOSLEEP==> will be fixed in GCC 4.8 _GLIBCXX_USE_CLOCK_MONOTONIC

Boost your knowledge Anthony Williams : - C++ Concurrency In Action Nicolai Josuttis : - The C++ Standard Library (second edition)