GameChoi
Choi Programming
GameChoi
전체 방문자
오늘
어제
  • 분류 전체보기 (468)
    • C++ Algorithm & Study (184)
      • C++ & Algorithm Strategies (45)
      • Game Math & DirectX 11 (72)
      • Server + UE5 (29)
      • Lyra Clone Coding (37)
    • Create Game (284)
      • [Window API] Game Client & .. (55)
      • [DirectX] DirectX 2D & 3D (155)
      • [UE5] BLUEPRINT & C++ (74)
    • odds and ends (0)
      • English (0)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

인기 글

태그

  • Game Room
  • Direct3D
  • Other Character
  • session
  • Network Worker
  • UE5
  • job queue
  • core
  • Player State
  • Algorithm Strategies
  • c++
  • Destination Move Packet
  • protobuf
  • server
  • RPG Game
  • Direct11
  • client
  • Player Move Packet
  • Game Server
  • GAME Client

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
GameChoi

Choi Programming

[SERVER] Thread
Create Game/[Window API] Game Client & Game Server

[SERVER] Thread

2023. 1. 3. 17:01

1. Thread

1.1 Thread

 - CPU 코어에서 돌아가는 프로그램 단위를 쓰레드(Thread) 라고 부름

 - CPU 의 코어 하나에서는 한 번에 한 개의 쓰레드의 명령을 실행

1.2 Process

 - 운영체제에서 실행되는 프로그램의 최소 단위이며 여러 개의 쓰레드로 이루어 지고 있음

 - 프로세스와 쓰레드의 차이는 메모리를 공유하느냐 하지 않느냐의 차이

1.3 Context Switching

 - 각 코어에서는 코어가 돌아가는데 컨텍스트 스위칭을 통해 쓰레드가 번갈아가며 실행

 - 컴퓨터에서 프로그램이 실행

   - 겉으로 보기에는 프로그램이 연속적으로 쭈르륵 작동하는 것 처럼 보이지만 실제로는 X

     - 아래 표을 보면 CPU 코어 하나에서 프로그램들이 어떻게 실행되는지 알 수 있음

게임1 음악1 인터넷1 음악2 게임2  

     - 프로그램이 하나가 쭈르륵 작동하는 것이 나닌 프로그램 하나가 잠시 실행되었다가 다른 프로그램으로 스위칭 됨

 - CPU는 한 프로그램을 통째로 쭉 실행시키는 것이 아닌 프로그램을 조금씩 골라서 차례를 돌며 실행시키는 것

   - CPU는 운영체제가 처리하라고 시키는 명령어들을 실행, 어떠한 것을 하는 것은 운영체제의 스케쥴러가 결정

 

2. 병렬화 - Parallelizable

2.1 덧셈

 - 쓰레드를 사용하여 1부터 100까지의 덧셈을 실행

int result = 0; // 초기 값
void SumThread(int start, int end)
{
    cout << start << " ~ " << end << endl;
    for (int i = start; i < end; i++) result += i;
}

int main()
{
    vector<std::thread> sumThread;
    int sum = 0;

    for (int i = 0; i < 10; i++) sumThread.push_back(std::thread(SumThread, (i * 10), ((i + 1) * 10)));
    for (int i = 0; i < 10; i++) sumThread[i].join();
    
    cout << result << endl;
}

2.2 뺄셈

 - 쓰레드를 사용하여 1부터 100까지 더한 값을 뺌

int result = 4950; // 1부터 100까지 더한 값
void SumThread(int start, int end)
{
    for (int i = start; i < end; i++) result -= i;
    cout << start << " ~ " << end << endl;
}

int main()
{
    vector<std::thread> sumThread;
    int sum = 0;

    for (int i = 0; i < 10; i++) sumThread.push_back(std::thread(SumThread, (i * 10), ((i + 1) * 10)));
    for (int i = 0; i < 10; i++) sumThread[i].join();
    
    cout << result << endl;
}

'Create Game > [Window API] Game Client & Game Server' 카테고리의 다른 글

[SERVER] MUTEX  (0) 2023.01.03
[SERVER] Race Condition  (0) 2023.01.03
[Game Client] 9 - UI  (0) 2022.12.31
[Game Client] 8 - Tile Map Save & Load  (0) 2022.12.29
[Game Client] 7 - Tile Map  (0) 2022.12.29
    'Create Game/[Window API] Game Client & Game Server' 카테고리의 다른 글
    • [SERVER] MUTEX
    • [SERVER] Race Condition
    • [Game Client] 9 - UI
    • [Game Client] 8 - Tile Map Save & Load
    GameChoi
    GameChoi

    티스토리툴바