4월, 2022의 게시물 표시

c++11 thread, condition_variable and async 개념정리

카테고리 C/C++ 글모음 개요 c++11 의 thread, condition_variable(이하 cv), async 뭐 이런 것들 공부하다가 머릿속이 꼬였습니다. cv와 async를 비교하고 있더라구요. 그래서 개념을 명확하게 하고 헷갈리는 부분을 정리해봤습니다. condition_variable 먼저 condition_variable(이하 cv) 에 대해서 명확하게 의미를 정의하고 넘어가야겠네요. The condition_variable class is a synchronization primitive that can be used to block a thread, or multiple threads at the same time, until another thread both modifies a shared variable (the condition), and notifies the condition_variable. 출처 :  cppreference.com std::condition_variable cv는 동기화의 기본 요소이다. 쉽게 말해서 맘대로 돌고있는 둘 이상의 쓰레드에 대해 서로 동기화 시켜줄 수 있는 방법이라는거죠. async와 비교해야할 대상이 아니었습니다. async The function template async runs the function f asynchronously 출처 : cppreference.com std::condition_variable   function f를 비동기적으로 실행시켜주는 함수 템플릿이다. async를 통해 task를 생성하여 비동기적으로 multi-threading을 구현해주는 함수 템플릿인거죠. 그러니까 task화한 thread인거죠. 헷갈린 부분 첫번째는, cv는 sync(동기)로 설명하고 있고, async는 async(비동기)로 설명하고 있어서 둘이 대비되는 기능인 것 처럼 비교하고 있었습니다. 동기화의 주체, 비동기화의 주체가 달라서 비교할 대상이 아닌데 말이죠.  두번째는, thread cv

condition_variable 테스트

서론 c++11 의 condition_variable 에 대해서는 유투브 코드없는 프로그래밍 의 강의 를 보시는 것을 추천합니다. 가능하면 관련 강의를 모두 들으시기 바랍니다. provider/consumer 패턴을 사용할 일이 많기 때문에, condition_variable 에 대해 보다 확실하기 위한 테스트 입니다. 준비 void wait( std::unique_lock<std::mutex>& lock, Predicate stop_waiting ); condition_variable의 메소드 wait() 에 파라메터로 들어가는 stop_waiting function 조건에 대해서 테스트 코드 결과 및 분석 결과1 main.cpp   24 provider : notify value:1 main.cpp   37 operator() : running:1 value:1 return:0 main.cpp   24 provider : notify value:2 main.cpp   37 operator() : running:1 value:2 return:0 main.cpp   24 provider : notify value:3 main.cpp   37 operator() : running:1 value:3 return:0 main.cpp   24 provider : notify value:4 main.cpp   37 operator() : running:1 value:4 return:0 main.cpp   24 provider : notify value:5 main.cpp   37 operator() : running:1 value:5 return:0 main.cpp   24 provider : notify value:6 main.cpp   37 operator() : running:1 value:6 return:0 main.cpp   24 provider : notify value:7 main.cpp   37 operator() : running:1

이 블로그의 인기 게시물

WSL2 Ubuntu 20.04 및 네트워크 설정

git pull 을 했더니 branch가 갈라지는 경우

리눅스 멀티코어를 사용하는 tar 압축/해제