-
6장 동시성독서/실용주의 프로그래머 2022. 3. 30. 09:15
TIL 요약
"공유 상태는 틀린 상태"
"불규칙한 실패는 동시성 문제인 경우가 많다."
TIL 날짜
2022.03.30
오늘 읽은 범위
6장 - 동시성
내가 지금까지 했던 코딩은 행동과 행동이 연결된 코딩들이었는데 그래서 어느 한 곳에서 에러가 난다면 멈추거나 혹은 예외처리로 다른 행동을 하거나 였다. 이번 6장에서는 어떻게 프로그래밍을 동시적으로 작성할 수 있는지, 그리고 예전부터 어떤 문제를 맞닿뜨려 왔고 해결해왔는지를 알 수 있는 장이었다. 먼저 병렬적이라는 단어와 동시성이라는 단어의 차이는 하드웨어의 입장이냐 소프트웨어의 입장이냐의 차이가 있다고 한다. 그래서 프로그래밍에서는 동시성이라는 단어를 쓴다고 한다. 먼저 기존의 코드를 동시성을 띈 소스코드로 만들기 위해서는 동시성을 찾는 과정으로부터 시작한다. 책에서는 활동 다이어그램을 이용해 분석을 한다고 한다. 여러 가지 기호가 있고 전체적인 그림은 아래와 같다.
그리고 나서 액터 모델들을 설계해본다. 액터 모델이란 어떤 행동을 하는 하나의 개체로써 흐름의 중간에 있는 것이 아니라 마치 식당에서 일을 하는 각각의 요소로서 행동한다. (예를 들면 종업원, 고객, 혹은 매니저가 있을 것이다. 이 부분에서 예전에 몇 페이지 읽다가 지금까지 완독을 못한 "Nature of Code"라는 책이 생각났다.) 만약 각 요소(액터)가 공유된 데이터에 접근할 경우에는 어떻게 될까? 책에서는 두 개의 액터가 동시에 한 변수를 바라보고 각자가 할 일을 하고 최종적으로 변수를 변화시킬 때 그 결과에 대해 문제점을 보여주었다. 세마포어라는 해결법을 사용하였고 어느 한 액터가 먼저 데이터에 접근을 하여 세마포어 상태를 사용 중 바꾸고 그 사이에 다른 액터는 세마포어가 대기 중이 될 때까지 그 데이터에 접근할 수 없게 하는 방법을 사용하였다.
나는 아직 동시성을 띈 코드를 직접 작성해보지는 않았지만 사용하는 언어에서 나도 모르게 사용되고 있을지도 모른다는 생각을 했다. 언젠간 동시성에 대해 고민해보고 작성할 날을 생각하며 오늘의 방식들을 기억해야겠다.
728x90반응형LIST'독서 > 실용주의 프로그래머' 카테고리의 다른 글
8장 프로젝트 전에 (0) 2022.04.03 7장 코딩하는 동안 (0) 2022.04.02 Quiz (0) 2022.03.27 5장 구부러지거나 부러지거나 (0) 2022.03.27 4장 실용주의 편집증 (0) 2022.03.24