Concurrency in C++


Download Concurrency in C++


Preview text

CONCURRENCY IN C++
Yuqing Xia CSCI 5828 Prof. Kenneth M. Anderson University of Colorado at Boulder

OUTLINE
 Introduction  Importance of Concurrency  Threads launch  Protection on shared data
 Atomic  Mutex
 Communication
 Condition variables
 Memory model
 Definition  Operaton orders

INTRODUCTION-WHY CONCURRENCY IS NECESSARY
 Processor speed has slowed and we can use transistors from Moore’s law for parallelism to increase speed.
 Concurrent programming is necessary to utilize parallel hardware.
 Sometimes it is natural to describe a problem with multi-threads just like divide a problem into several steps.

CONCURRENCY INTRODUCED TO C++11
 Original C++ Stan-dard was published in 1998 only support single thread programming
 The new C++ Standard (referred to as C++11 or C++0x) was published in 2011. It will acknowledge the existence of multithreaded programs. Memory models for concurrency is also introduced.

WHY WRITE CONCURRENT PROGRAMS
 Dividing a problem into multiple executing threads is an important programming technique.
 Multiple executing threads may be the best way to describe a problem
 With multiple executing threads, we can write highly efficient program by taking advantage of any parallelism available in computer system.

CONCURRENCY REQUIREMENT ON PROGRAMMING LANGUAGE
 Thread creation – be able to create another thread of control.  Thread synchronization – be able to establish timing relationships
among threads.
 One thread waits until another thread has reached a certain point in its code.  One threads is ready to transmit information while the other is ready to receive the
message, simultaneously.
 Thread communication – be able to correctly transmit data among threads.

THREAD CREATION
 C++11 introduced a new thread library including utilities for starting and managing threads.
 Creating an instance of std::thread will automatically start a new thread.
Two thread will be created. The main thread will launch a new thread when it encounter the code std::thread th( ) to executive the function threadFucntion();
 The join function here is to force the current thread to wait for the thread to th finish. Otherwise the main function may exit without the thread th finished

CRITICAL SECTION
 Data are usually shared between threads. There is a problem when multiple threads attempting to operate on the same object simultaneously.
 If the operation is atomic(not divisible) which means no other thread can modify any partial results during the operation on the object, then it is safe. Otherwise, we are in a race condition.
 a critical section is a piece of code that accesses a shared resource (data structure or device) that must not be concurrently accessed by more than one thread of execution
 Preventing simultaneous execution of critical section by multiple thread is called mutual exclusion.

EXAMPLE
 Shared objects between threads will lead synchronization issues. For example
5 threads created try to increase the counter 5000 times. This program has a synchronization problem. Here are some result obtained on my computer: 24138 20326 23345 25000 17715 It is not the same every time.

PROTECT SHARED DATA
 The problem is that the increment is not an atomic operation
 Atomic operation: during which a processor can simultaneously read a location and write it in the same bus operation. Atomic implies indivisibility and irreducibility, so an atomic operation must be performed entirely or not performed at all.
 The increment in the example is made of three operations:
 Read the current value of vaue  Add one to the current value  Write that new value to value
 So when you launch more than one thread, they might interleave with each other and make the result impossible to predict.

Preparing to load PDF file. please wait...

0 of 0
100%
Concurrency in C++