본문 바로가기
반응형

💻 programming/c++26

[c++] std::shared_mutex en.cppreference.com/w/cpp/thread/shared_mutex std::shared_mutex - cppreference.com class shared_mutex; (since C++17) The shared_mutex class is a synchronization primitive that can be used to protect shared data from being simultaneously accessed by multiple threads. In contrast to other mutex types which facilitate exclusive access, a sh en.cppreference.com 상호배타적인 다른 mutex들과 달리 shard_mutex는 공유 자.. 2020. 12. 16.
[c++] std::timed_mutex en.cppreference.com/w/cpp/thread/timed_mutex std::timed_mutex - cppreference.com class timed_mutex; (since C++11) The timed_mutex class is a synchronization primitive that can be used to protect shared data from being simultaneously accessed by multiple threads. In a manner similar to mutex, timed_mutex offers exclusive, non-recursive en.cppreference.com mutex와 비슷하게, timed_mutex는 상호배타적이고, 비재귀적인 .. 2020. 12. 16.
[c++] std::recursive_mutex (std::mutex 비교) 일반적으로 mutex의 lock을 두 번 호출하면 무한 대기 상태에 빠지지만, recursive mutex는 lock을 여러 번 호출해도 문제가 없다 lock을 호출하는 동안은 계속 소유권을 가지고 있다가 lock을 호출한 횟수 만큼 unlock을 호출하면 소유권을 해제하기 때문이다 재귀함수를 만들어보면 쉽게 이해할 수 있다 (이름부터 recursive_mutex) 한 번 재귀함수를 병렬처리해보자 그냥 mutex를 사용했을 때 (무한 대기) worker 함수를 호출하는 4개의 스레드를 생성한다 worker 함수는 자기 자신을 다시 호출하는 재귀 함수로 구성한다 result라는 공유자원에 다른 스레드가 접근하지 못하도록 mutex lock을 수행한다 worker 내부에서 worker 함수를 다시 호출하므로.. 2020. 12. 16.
[c++] std::mutex en.cppreference.com/w/cpp/thread/mutex std::mutex - cppreference.com class mutex; (since C++11) The mutex class is a synchronization primitive that can be used to protect shared data from being simultaneously accessed by multiple threads. mutex offers exclusive, non-recursive ownership semantics: A calling thread owns a mut en.cppreference.com mutex는 여러 스레드의 공유 자원을 보호하기 위해 사용되는 클래스입니다. std::mute.. 2020. 12. 16.
[c++] mutex, lock guard, unique lock, shared mutex, recursive mutex medium.com/swlh/c-mutex-write-your-first-concurrent-code-69ac8b332288 [C++] MUTEX: Write Your First Concurrent Code Learn to design concurrent code and to protect shared with a mutex by implementing your first thread-safe queue! medium.com Mutex가 뭘까? Mutex는 여러 스레드에서 동시에 접근할 수 있는 공유 자원의 소유권을 결정하는 모델이다 Mutex를 이용해 다른 스레드에서 이 공유 자원에 접근하지 못하게 막을 수 있다는 의미다 만약 스레드 A에서 lock을 호출하면 unlock이 수행될 때까지 다른 스레드 .. 2020. 12. 16.
[c++] template meta programming Template Meta Programming 다음 c++ 프로그램의 동작을 예상해봅시다. #include using namespace std; template struct funStruct { enum { val = 2*funStruct ::val }; }; template struct funcStruct { enum {val = 1}; }; int main() [ cout 2020. 12. 15.
[c++] variadic template (가변 길이 템플릿) Variadic Template Variadic function templates in C++ - GeeksforGeeks 가변 길이 템플릿 (Variadic template)은 여러개의 인자를 가질 수 있습니다. 예를 들어 가변 길이 템플릿을 이용하여 정의한 함수 템플릿은 정해진 숫자가 아닌 가변 길이 인자를 받을 수 있습니다. Syntax for a variadic function template 아래 코드에서 typename 뒤에 선언된 "..."이 템플릿 파라미터 팩 (parameter pack)입니다. 이 템플릿 파라미터 팩을 선언하면 이 템플릿은 0개 이상의 (가변된 길이의) 인자를 받을 수 있다는 뜻입니다. template (typename arg, typename... args) retur.. 2020. 12. 15.
[c++] functor (function object) Functor (Function object) Functors in C++ - GeeksforGeeks 우선 Functor는 Function과 다르다는 것을 먼저 밝힙니다. 그럼 Functor와 Function은 무엇이 다를까요? 한 번 하나의 인자를 받는 함수를 생각해보세요. 만약 런타임 시간 동안 이 함수에 전달하고 싶은 데이터가 있다면 어떻게 전달할 수 있을까요? 간단한 방법은 전역 변수를 사용하는 것입니다. 그렇지만 누구나 알고 있듯 전역 변수를 사용하는 것은 다른 대안이 없을 때 정말 마지막으로 사용하는 것이 좋습니다. 함수 객체 (Functor; Function object)를 이용하면 이 문제를 해결하면서도 마치 함수나 함수 포인터인 것처럼 다룰 수 있습니다. 이러한 함수 객체를 이용한 방법.. 2020. 12. 15.
[c++] template specialization Template Specialization Template Specialization in C++ - GeeksforGeeks 템플릿을 이용하면 데이터 타입에 무관한 함수 또는 클래스를 만들 수 있습니다. 그런데 만약 특정 데이터 타입에 대해서만 다른 코드를 작성하고 싶으면 어떡해야 할까요? 예제 코드 함수 템플릿 예제 #include using namespace std; template void fun(T a) { cout 2020. 12. 15.
반응형