포스트

Concurrent

Concurrent

Concurrent

Write your content here.

Concurrent API

#include<thread> : 스레드 생성 및 조작 #include<mutex> : 멀티 스레드 환경 동기화 #include<condition_variable> : 조건 변수

std::thread 클래스는 스레드를 생성하고 관리하는 데 사용됩니다. 주요 함수 및 메서드: std::thread(ThreadFunc&& func, Args&&… args) : 스레드 생성 join() : 스레드가 종료될 때까지 기다림 detach() : 스레드가 종료되더라도 종료 대기 없이 관리에서 해제됨

std::mutex 클래스는 멀티 스레드 환경에서 동기화를 위한 객체입니다. 주요 함수 및 메서드: lock() : 뮤텍스를 획득 unlock() : 뮤텍스를 해제 std::lock_guard<std::mutex> : 자동으로 뮤텍스를 잠금 해제하는 스마트 포인터

std::condition_variable 클래스는 스레드 간의 동기화를 위한 객체로, 특정 조건이 만족될 때까지 스레드를 잠시 멈출 수 있게 해줍니다. 주요 함수 및 메서드: wait(std::unique_lock<std::mutex>& lock) : 특정 조건이 충족될 때까지 스레드를 잠금 notify_one() : 하나의 스레드에게 조건이 충족되었음을 알림 notify_all() : 모든 스레드에게 조건이 충족되었음을 알림

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <thread>

void task1(const std::string& name) {
    for (int i = 0; i < 5; ++i) {
        std::cout << name << " 가 작업을 수행중입니다." << i << "\n";
        std::this_thread::yield(); // 다른 스레드에게 CPU 양보
    }
}

void task2(const std::string& name) {
    for (int i = 0; i < 5; ++i) {
        std::cout << name << " 가 작업을 수행중입니다." << i << "\n";
        std::this_thread::yield(); // 다른 스레드에게 CPU 양보 (1)
    }
}

int main() {
    std::thread t1(task1, "Thread 1");
    std::thread t2(task2, "Thread 2");

    t1.join();
    t2.join();

    return 0;
}

cmd 출력

Thread 1 가 작업을 수행중입니다.0
Thread 2 가 작업을 수행중입니다.0
Thread 1 가 작업을 수행중입니다.1
Thread 2Thread 1 가 작업을 수행중입니다.2
 가 작업을 수행중입니다.1
Thread 1 가 작업을 수행중입니다.3
Thread 1 가 작업을 수행중입니다.4
Thread 2 가 작업을 수행중입니다.2
Thread 2 가 작업을 수행중입니다.3
Thread 2 가 작업을 수행중입니다.4