
전체 글

[SERVER] Condition Variable
1. Condition Variable 1.1 Producer & Consumer - 생산자 및 소비자 패턴을 사용할 때 어떠한 조건이 만족할 때까지 자라는 명령을 하지 못함 - 따라서 sleep을 이용해 재웠다 깨웠다 사용 1.2 Condition Variable - 조건 변수를 사용하면 이를 해결 가능 1.2.1 Main - Condition Variable Create int main() { vector workers; queue 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::th..

[SERVER] Producer & Consumer
1. Producer & Consumer 1.1 Producer & Consumer - 멀티 쓰레드 프로그램에서 가장 많이 등장하는 패턴 - 생산자는 무언가 처리할 일을 받아오는 쓰레드를 의미 - 소비자는 받은 일을 처리하는 쓰레드를 의미 - Queue 사용 - FIFO - 먼저 처리할 일을 받으면 먼저 처리함 1.2 Program 1.2.1 Main - Queue, Mutex Create - 일감을 저장할 수 있게 큐 생성 및 상호배제를 하기 위해 Mutex 생성 int main() { vector workers; queue q; std::mutex m; workers.push_back(std::thread(producer, std::ref(q), std::ref(m))); workers.push_ba..

[SERVER] Dead Lock
1. Dead Lock 1.1 Dead Lock void worker1(int& counter, std::mutex& m1, std::mutex& m2) { for (int i = 0; i < 10000; i++) { std::lock_guard lock1(m1); std::lock_guard lock2(m2); counter++; } } void worker2(int& counter, std::mutex& m1, std::mutex& m2) { for (int i = 0; i < 10000; i++) { std::lock_guard lock1(m2); std::lock_guard lock2(m1); counter++; } } int main() { vector workers; std::mutex m1;..