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
  • job queue
  • Game Server
  • core
  • protobuf
  • Player Move Packet
  • Network Worker
  • Direct11
  • GAME Client
  • RPG Game
  • c++
  • Destination Move Packet
  • Algorithm Strategies
  • session
  • client
  • Other Character
  • Player State
  • UE5
  • Direct3D
  • server

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
GameChoi

Choi Programming

[SERVER] Condition Variable
Create Game/[Window API] Game Client & Game Server

[SERVER] Condition Variable

2023. 1. 3. 18:51

1. Condition Variable

1.1 Producer & Consumer

 - 생산자 및 소비자 패턴을 사용할 때 어떠한 조건이 만족할 때까지 자라는 명령을 하지 못함

   - 따라서 sleep을 이용해 재웠다 깨웠다 사용

1.2 Condition Variable

 - 조건 변수를 사용하면 이를 해결 가능

 

1.2.1 Main

 - Condition Variable Create

int main()
{
    vector<std::thread> workers;
    queue<int> q;
    std::mutex m;
    std::condition_variable cv;
   
    workers.push_back(std::thread(producer, std::ref(q), std::ref(m), std::ref(cv)));
    workers.push_back(std::thread(consumer, std::ref(q), std::ref(m), std::ref(cv)));
    
    for (int i = 0; i < 2; i++) workers[i].join();
    cv.notify_all(); // producer가 끝이나면 잠자고 있는 모든 쓰레드를 깨움
}

1.2.2 Producer

 - notify_one()

   - 잠자고 있는 쓰레드들 중 하나를 개워서 일을 시켜야 함

     - 조건이 거짓인 바람에 자고 있는 쓰레드 중 하나를 깨워 조건을 다시 검사하게 함

void producer(queue<int>& q, std::mutex& m, std::condition_variable& cv)
{
    for (int i = 0; i < 100; i++)
    {
        unique_lock<mutex> lock(m);
        q.push(i * 10);
        cv.notify_one();
    }
}

1.2.3 Consumer

 - wait

   - 어떤 조건이 참이 될 때까지 기다리는 함수

     - 큐가 비어있을 때 중지하고 누가 깨워주기 전까진 sleep 상태 유지

void consumer(queue<int>& q, std::mutex& m, std::condition_variable& cv)
{
    while(true)
    {
        unique_lock<mutex> lock(m);

        cv.wait(lock, [&]() {return q.empty() == false; });
        int data = q.front();
        q.pop();
        cout << data << endl;
    }
}
저작자표시 (새창열림)

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

[SERVER] Memory Order  (0) 2023.01.05
[SERVER] Atomic  (0) 2023.01.05
[SERVER] Producer & Consumer  (0) 2023.01.03
[SERVER] Dead Lock  (0) 2023.01.03
[SERVER] MUTEX  (0) 2023.01.03
    'Create Game/[Window API] Game Client & Game Server' 카테고리의 다른 글
    • [SERVER] Memory Order
    • [SERVER] Atomic
    • [SERVER] Producer & Consumer
    • [SERVER] Dead Lock
    GameChoi
    GameChoi

    티스토리툴바