SlideShare a Scribd company logo
1 of 12
Download to read offline
Effective Modern C++ Study
C++ Korea
발표자 : 윤석준
Effective Modern C++ Study
C++ Korea3
std::atomic<int> A(0);
A = 10;
std::cout << A;
++A;
--A;
 std::atomic<T>은 다른 Thread로부터 atomic 하게 작업하는 것을 보장
 mutex와 비슷하지만 lock-free로 동작 가능 (컴파일러에 따라 다름)
 다른 Thread에서 A를 읽을 때 0, 10, 11 값 중 하나
 RMW 연산 (Read-Modify-Write) 및 모든 member function은
multi-thread 환경에서 atomic 동작
Effective Modern C++ Study
C++ Korea4
volatile int V(0);
A = 10;
std::cout << A;
++A;
--A;
 다른 Thread에서 값을 읽을 때 아무 것도 보장해 주지 않는다.
 경쟁 상태 (Data Race)가 발생 할 수 있다.
 다른 Thread에서 A를 읽을 때 0, 10, 11 값이 아닐 수 있음
Effective Modern C++ Study
C++ Korea5
std::atomic<int> x(0);
std::atomic<int> y(x.load());
y.store(x.load());
 COPY 연산자가 없다.
 std::atomic<T>에 대한 MOVE 생성자, 연산자도 없다.
 값을 다른 곳으로 전달하기 위해서는 load() 와 store() 함수를 이용한다.
 std::atomic<T>에 대해서 값을 읽고 쓰는 동작은 load() 와 store() 함수를 이용하자.
(그러면 소스코드상에서 std::atomic<T>와 일반 변수를 구별하기 쉬워진다.)
Effective Modern C++ Study
C++ Korea6
 Code Reorder
a = b;
x = y;
x = y;
a = b;
 Redundant loads  Dead Stores
auto y = x;
= x;
auto y = x;
x = 10;
 Reordering은 Compile-time 뿐 아니라 Run-time에서 다른 Core에 할당도 가능하다.
Effective Modern C++ Study
C++ Korea7
 std::atomic<int>
Not Permitted
std::atomic<bool> isAvailable(false);
int Value = Func();
isAvailable = true;
 volatile
Permitted
volatile isAvailable(false);
int Value = Func();
isAvailable = true;
volatile isAvailable(false);
isAvailable = true;
int Value = Func();
Effective Modern C++ Study
C++ Korea8
 std::atomic<int>
Permitted
auto y = x;
y = x;
x = 10;
x = 20;
 volatile
Not Permitted
auto y = x;
x = 20;
auto y = x;
y = x;
x = 10;
x = 20;
Effective Modern C++ Study
C++ Korea9
 Normal Memory
• 값을 기록하면, 다른 값을 쓰기 전까지 유지된다.
• 값을 기록하고, 읽지 않은 상태에서 다른 값을 기
록하면 그 전에 값은 제거된다.
 Special Memory
e.g. Memory mapping I/O
• 값을 기록하지 않아도 값이 변할 수 있다.
• 값을 기록하고, 읽지 않은 상태에서 다른 값을 기
록하더라도 그 두 값은 모두 의미가 있을 수 있다.
컴파일러에게 내가 바로 “Special Memory” 다.
라고 알려주는 방법은 ?
volatile
Effective Modern C++ Study
C++ Korea10
 std::atomic<int>
Concurrent Programming
volatile std::atomic<int> VA;
 volatile
Special Memory
그렇다면 Special Memory를 Concurrent Programming에서 사용하고자 한다면 ???
Effective Modern C++ Study
C++ Korea11
• std::atomic<T>은 Multi-Thread 환경에서 mutex를 사용하지 않고, data를 제어하는데 사용된다.
(즉, Concurrent Software를 위한 도구이다.)
• volatile 즉은 Read/Write 작업에 대해서 최적화를 하면 안 되는 data를 제어하는데 사용된다.
(즉, Special Memory를 위한 도구이다.)
http://devluna.blogspot.kr/2015/03/item-40-concurrency-stdatomic-volatile.html
icysword77@gmail.com

More Related Content

More from Seok-joon Yun

[2015 07-06-윤석준] Oracle 성능 최적화 및 품질 고도화 4
[2015 07-06-윤석준] Oracle 성능 최적화 및 품질 고도화 4[2015 07-06-윤석준] Oracle 성능 최적화 및 품질 고도화 4
[2015 07-06-윤석준] Oracle 성능 최적화 및 품질 고도화 4
Seok-joon Yun
 

More from Seok-joon Yun (20)

Retrospective.2020 03
Retrospective.2020 03Retrospective.2020 03
Retrospective.2020 03
 
Sprint & Jira
Sprint & JiraSprint & Jira
Sprint & Jira
 
Eks.introduce.v2
Eks.introduce.v2Eks.introduce.v2
Eks.introduce.v2
 
Eks.introduce
Eks.introduceEks.introduce
Eks.introduce
 
AWS DEV DAY SEOUL 2017 Buliding Serverless Web App - 직방 Image Converter
AWS DEV DAY SEOUL 2017 Buliding Serverless Web App - 직방 Image ConverterAWS DEV DAY SEOUL 2017 Buliding Serverless Web App - 직방 Image Converter
AWS DEV DAY SEOUL 2017 Buliding Serverless Web App - 직방 Image Converter
 
아파트 시세,어쩌다 머신러닝까지
아파트 시세,어쩌다 머신러닝까지아파트 시세,어쩌다 머신러닝까지
아파트 시세,어쩌다 머신러닝까지
 
Pro typescript.ch07.Exception, Memory, Performance
Pro typescript.ch07.Exception, Memory, PerformancePro typescript.ch07.Exception, Memory, Performance
Pro typescript.ch07.Exception, Memory, Performance
 
Doing math with python.ch07
Doing math with python.ch07Doing math with python.ch07
Doing math with python.ch07
 
Doing math with python.ch06
Doing math with python.ch06Doing math with python.ch06
Doing math with python.ch06
 
Doing math with python.ch05
Doing math with python.ch05Doing math with python.ch05
Doing math with python.ch05
 
Doing math with python.ch04
Doing math with python.ch04Doing math with python.ch04
Doing math with python.ch04
 
Doing math with python.ch03
Doing math with python.ch03Doing math with python.ch03
Doing math with python.ch03
 
Doing mathwithpython.ch02
Doing mathwithpython.ch02Doing mathwithpython.ch02
Doing mathwithpython.ch02
 
Doing math with python.ch01
Doing math with python.ch01Doing math with python.ch01
Doing math with python.ch01
 
Pro typescript.ch03.Object Orientation in TypeScript
Pro typescript.ch03.Object Orientation in TypeScriptPro typescript.ch03.Object Orientation in TypeScript
Pro typescript.ch03.Object Orientation in TypeScript
 
C++ Concurrency in Action 9-2 Interrupting threads
C++ Concurrency in Action 9-2 Interrupting threadsC++ Concurrency in Action 9-2 Interrupting threads
C++ Concurrency in Action 9-2 Interrupting threads
 
Welcome to Modern C++
Welcome to Modern C++Welcome to Modern C++
Welcome to Modern C++
 
[2015-07-20-윤석준] Oracle 성능 관리 2
[2015-07-20-윤석준] Oracle 성능 관리 2[2015-07-20-윤석준] Oracle 성능 관리 2
[2015-07-20-윤석준] Oracle 성능 관리 2
 
[2015-07-10-윤석준] Oracle 성능 관리 & v$sysstat
[2015-07-10-윤석준] Oracle 성능 관리 & v$sysstat[2015-07-10-윤석준] Oracle 성능 관리 & v$sysstat
[2015-07-10-윤석준] Oracle 성능 관리 & v$sysstat
 
[2015 07-06-윤석준] Oracle 성능 최적화 및 품질 고도화 4
[2015 07-06-윤석준] Oracle 성능 최적화 및 품질 고도화 4[2015 07-06-윤석준] Oracle 성능 최적화 및 품질 고도화 4
[2015 07-06-윤석준] Oracle 성능 최적화 및 품질 고도화 4
 

[C++ Korea] Effective Modern C++ Item 40 Use std::atomic for Concurrency, volatile for Special Memory. +윤석준

  • 1. Effective Modern C++ Study C++ Korea 발표자 : 윤석준
  • 2.
  • 3. Effective Modern C++ Study C++ Korea3 std::atomic<int> A(0); A = 10; std::cout << A; ++A; --A;  std::atomic<T>은 다른 Thread로부터 atomic 하게 작업하는 것을 보장  mutex와 비슷하지만 lock-free로 동작 가능 (컴파일러에 따라 다름)  다른 Thread에서 A를 읽을 때 0, 10, 11 값 중 하나  RMW 연산 (Read-Modify-Write) 및 모든 member function은 multi-thread 환경에서 atomic 동작
  • 4. Effective Modern C++ Study C++ Korea4 volatile int V(0); A = 10; std::cout << A; ++A; --A;  다른 Thread에서 값을 읽을 때 아무 것도 보장해 주지 않는다.  경쟁 상태 (Data Race)가 발생 할 수 있다.  다른 Thread에서 A를 읽을 때 0, 10, 11 값이 아닐 수 있음
  • 5. Effective Modern C++ Study C++ Korea5 std::atomic<int> x(0); std::atomic<int> y(x.load()); y.store(x.load());  COPY 연산자가 없다.  std::atomic<T>에 대한 MOVE 생성자, 연산자도 없다.  값을 다른 곳으로 전달하기 위해서는 load() 와 store() 함수를 이용한다.  std::atomic<T>에 대해서 값을 읽고 쓰는 동작은 load() 와 store() 함수를 이용하자. (그러면 소스코드상에서 std::atomic<T>와 일반 변수를 구별하기 쉬워진다.)
  • 6. Effective Modern C++ Study C++ Korea6  Code Reorder a = b; x = y; x = y; a = b;  Redundant loads  Dead Stores auto y = x; = x; auto y = x; x = 10;  Reordering은 Compile-time 뿐 아니라 Run-time에서 다른 Core에 할당도 가능하다.
  • 7. Effective Modern C++ Study C++ Korea7  std::atomic<int> Not Permitted std::atomic<bool> isAvailable(false); int Value = Func(); isAvailable = true;  volatile Permitted volatile isAvailable(false); int Value = Func(); isAvailable = true; volatile isAvailable(false); isAvailable = true; int Value = Func();
  • 8. Effective Modern C++ Study C++ Korea8  std::atomic<int> Permitted auto y = x; y = x; x = 10; x = 20;  volatile Not Permitted auto y = x; x = 20; auto y = x; y = x; x = 10; x = 20;
  • 9. Effective Modern C++ Study C++ Korea9  Normal Memory • 값을 기록하면, 다른 값을 쓰기 전까지 유지된다. • 값을 기록하고, 읽지 않은 상태에서 다른 값을 기 록하면 그 전에 값은 제거된다.  Special Memory e.g. Memory mapping I/O • 값을 기록하지 않아도 값이 변할 수 있다. • 값을 기록하고, 읽지 않은 상태에서 다른 값을 기 록하더라도 그 두 값은 모두 의미가 있을 수 있다. 컴파일러에게 내가 바로 “Special Memory” 다. 라고 알려주는 방법은 ? volatile
  • 10. Effective Modern C++ Study C++ Korea10  std::atomic<int> Concurrent Programming volatile std::atomic<int> VA;  volatile Special Memory 그렇다면 Special Memory를 Concurrent Programming에서 사용하고자 한다면 ???
  • 11. Effective Modern C++ Study C++ Korea11 • std::atomic<T>은 Multi-Thread 환경에서 mutex를 사용하지 않고, data를 제어하는데 사용된다. (즉, Concurrent Software를 위한 도구이다.) • volatile 즉은 Read/Write 작업에 대해서 최적화를 하면 안 되는 data를 제어하는데 사용된다. (즉, Special Memory를 위한 도구이다.)