GameChoi 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;
    }
}