[OS] Thread 정리 1

Thread

4.1 개요

단일스레드 다중스레드

단일 스레드

멀티 스레드

4.1.2 동기

4.1.3 장점

4.2 다중(멀티) 코어 프로그래밍

다중코어란 동일한 성능으로 작동하는 CPU 여러 개를 1개의 칩 속에 집적해 놓은 것이다. (컴퓨터 내에서 실질적인 연산처리를 수행하는 CPU가 여러개의 코어로 구성되어 있다는 것을 의미.)


[참고] Concurrency !== Parallelism

https://dev.to/roperzh/concurrency--parallelism-55h5

Concurrency (동시성 / 병행)

모든 태스크가 징행하게끔 함으로써 하나 이상의 태스크를 수행시킨다.

Concurrency is all about doing tasks in the same interval of time. The important detail here is that the tasks don’t necessarily execute at the same time, but they can be divided into smaller tasks that can be interleaved.

concurrency

A good place where concurrency takes place is in the kitchen, imagine a single chef cutting lettuce while is also checking from time to time something in the oven. He has to stop cutting, check the oven, stop checking the oven and then start cutting again, and repeat the process until is done.

As you can see, concurrency is mostly related to the logistics, without concurrency, the chef will have to wait until the meat in the oven is ready in order to cut the lettuce.

Parallelism (병렬)

하나 이상의 태스크를 동시에 수행할 수 있는 시스템

Parallelism, on the other hand, is about doing tasks literally at the same time, as the name implies they are executed in parallel.

parallelism-bw

Back in the kitchen again, now we have two chefs, one can take care of the oven, while the otter cuts the lettuce, we split the work at the expence of having another chef.

Parallelism is a subclass of concurrency: before you execute multiple tasks simultaneously, you first have to manage multiple tasks.

4.2.2 병렬 실행의 유형

병렬 실행에는 크게 데이터 병렬 실행태스크 병렬 실행 두 가지가 있다.

4.3 다중 스레드 모델

스레드는 사용자 수준의 사용자 스레드, 커널 수준의 커널 스레드가 제공된다.
사용자 스레드는 커널 위에서 지원되며, 커널 스레드는 운영체제에 의해 지원되고 관리된다.

다대일 모델

일대일 모델

다대다 모델

4.4 스레드 라이브러리

프로그래머에게 스레드를 생성하고 관리하기 위한 API를 제공

스레드 라이브러리 구현 방법

1) 커널의 지원없이 완전히 사용자 공간에서만 라이브러리를 제공하는 것

2) 운영체제에 의해 지원되는 커널 수준 라이브러리를 구현하는 것

### Pthreads

POSIX(IEEE 1003.1c)가 스레드 생성과 동기화를 위해 제정한 표준 API

Java 스레드(Java Thread)

​ 1) Thread 클래스로부터 파생된 새로운 클래스를 생성하고, Thread 클래스의 run() 매서드를 무효화(override)하는 것

​ 2) Runnable 인터페이스를 구현하는 클래스 정의