λκΈ°μ λΉλκΈ°
λκΈ°
μμ μ μμ°¨μ μΌλ‘ μ§ννλ λ°©μ
νλμ μμ μ΄ λλ λκΉμ§ λ€μ μμ μ μ€νλμ§ μκ³ κΈ°λ€λ¦°λ€.
μμ : μμ A -> μμ B -> μμ C (Aκ° λλ λκΉμ§ B, Cλ μ€νλμ§ μμ)
λΉλκΈ°
μμ μ κΈ°λ€λ¦¬μ§ μκ³ λμμ μ§ν
μμ μ μμ²νλ©΄ κ·Έ μμ μ΄ μλ£λ λκΉμ§ κΈ°λ€λ¦¬μ§ μκ³ , λ€λ₯Έ μμ μ κ³μ μ§ν
μμ : μμ A -> μμ B -> μμ C (Aκ° μ§νλλ λμ Bμ Cλ λμμ μ€νλ μ μμ)
λΈλ‘νΉκ³Ό λ ΌλΈλ‘νΉ
λΈλ‘νΉ
μμ μ΄ μλ£λ λκΉμ§ μ μ΄κΆμ λ°ννμ§ μλ λ°©μ
μ΄λ ν μμ μ΄ λλ λκΉμ§ κΈ°λ€λ Έλ€κ° μμ μ μννλ κ²½μ°
μμ : μμ A -> λκΈ° -> μμ B -> λκΈ° -> μμ C (κ° μμ μ΄ λλ λκΉμ§ λκΈ°)
λ ΌλΈλ‘νΉ
μμ μ΄ μ§νλλ λμμλ μ μ΄κΆμ λ°ννκ³ λ€λ₯Έ μμ μ κ³μ μ§ν
λ€λ₯Έ μμ κ³Ό μκ΄μμ΄ μμ μ μμ μ μννλ κ²
μμ : μμ A μ§ν μ€ -> μμ B μμ -> μμ C μμ (κ° μμ μ λ 립μ μΌλ‘ μ€ν)
μ¦, λκΈ°μ λΉλκΈ°λ μμ μ μμλ₯Ό μμ°¨μ μΌλ‘ ν κ²μ΄λ, μμ°¨μ μ΄μ§μκ² μ§νν κ²μ΄λλ₯Ό μ΄μΌκΈ°νλκ²μ΄κ³ , λΈλ‘νΉ λ ΌλΈλ‘νΉμ μ΄λ€ν μμ μ μ§νν΄μΌν λ, μμ μ λν΄ μ μ΄κΆμ κ°μ§κ³ νΈλ€λ§νλκ²μ μ΄μΌκΈ°νλ€.
λκΈ°μ λΉλκΈ° : μμ μ μμ
λΈλ‘νΉκ³Ό λ ΌλΈλ‘νΉ : μ μ΄κΆμ λ°νμ¬λΆ
μ½λλ₯Ό ν΅ν μμ (cpp)
λκΈ° λΈλ‘νΉ
void print(const std::string& message) {
std::cout << "[start] :" << message << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(2)); // 2s
std::cout << "[end] :" << message << std::endl;
}
...
print("hello!");
print("world!");
...
[start] :hello!
[start] :world!
[end] :hello!
[end] :world!
λκΈ° : ν¨μμ νΈμΆμ΄ λλ λκΉμ§ λ€μμ½λλ μ€νλμ§μλλ€.
λΈλ‘νΉ : νΈμΆλ ν¨μκ° μ€νλλ λμ μ μ΄κΆμ λ°νλ°μ§ λͺ»νλ€.
λΉλκΈ° λ ΌλΈλ‘νΉ
void print(const std::string& message) {
std::cout << "[start] :" << message << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(2)); // 2s
std::cout << "[end] :" << message << std::endl;
}
...
std::thread t1(print, "hello!");
std::thread t2(print, "world!");
t1.detach();
t2.detach();
std::cout << "Main thread start!" << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(3)); // 3s
std::cout << "Main thread end!" << std::endl;
...
[start] :hello!
[end] :hello!
Main thread start!
[start] :world!
[end] :world!
Main thread end!
λΉλκΈ° : threadλ₯Ό λ°νμΌλ‘ λ³λμ μ€λ λλ₯Ό ν΅ν΄ λμνμκΈ°λλ¬Έμ t1 μ΄ μ€νλλ λμ t2 λ λμμ μ€νμ΄ μ΄λ£¨μ΄μ§λ€.
λ ΌλΈλ‘νΉ : detachλ₯Ό μ¬μ©νμ¬ λ©μΈ μ€λ λμ λ 립μ μΌλ‘ μ€νμ΄ λλ€.
λκΈ° λ ΌλΈλ‘νΉ
μ½λλ₯Ό ν΅ν΄ νννκΈ°μ λ§€νλ€.
λκΈ°: μμ λ€μ΄ μμ°¨μ μΌλ‘ μ§νλλ―λ‘, κ° μμ μ μ΄μ μμ μ΄ μλ£λ λκΉμ§ κΈ°λ€λ¦½λλ€.
λ ΌλΈλ‘νΉ: μμ μ΄ μλ£λ λκΉμ§ κΈ°λ€λ¦¬μ§ μκ³ , λ€λ₯Έ μμ μ μ§νν μ μμ΅λλ€.
μμ κ°μ νΉμ§μ κ°μ§κ³ μκΈ°λλ¬Έμ λ κ°λ μ΄ λ³Έμ§μ μΌλ‘ μλ°λ μ±κ²©μ κ°μ§λ€.
λκΈ°λ μ½λμ μ§νμ μμ°¨μ μΌλ‘ νλ€λ ν° νμ κ°μ§κ³ μκΈ°λλ¬Έμ λ ΌλΈλ‘νΉμ νλ€κ³ ν΄λ λκΈ°λ‘ μΈν΄ 묻νλ²λ¦°λ€
λΉλκΈ° λΈλ‘νΉ
void print(const std::string& message) {
std::cout << "[start] :" << message << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(2)); // 2s
std::cout << "[end] :" << message << std::endl;
}
...
std::thread t1(print, "hello!");
std::thread t2(print, "world!");
t1.join();
t2.join();
std::cout << "Main thread start!" << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(3)); // 3s
std::cout << "Main thread end!" << std::endl;
...
[start] :hello!
[end] :hello!
[start] :world!
[end] :world!
Main thread start!
Main thread end!
λΉλκΈ° : threadλ₯Ό λ°νμΌλ‘ λ³λμ μ€λ λλ₯Ό ν΅ν΄ λμνμκΈ°λλ¬Έμ t1 μ΄ μ€νλλ λμ t2 λ λμμ μ€νμ΄ μ΄λ£¨μ΄μ§λ€.
λΈλ‘νΉ: join μΌλ‘ μΈνμ¬ λΈλ‘νΉμ²λ¦¬κ° λμ΄ join threadκ° λ§λ¬΄λ¦¬κ° λκΈ°μ κΉμ§ λμ΄κ° μ μλ€.
'CS' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
gRPC (2) | 2024.12.17 |
---|---|
process, thread (0) | 2024.12.14 |
ν΄λμ€ μΈμ€ν΄μ€ κ°μ²΄ (1) | 2024.12.14 |
TCPμ UDP ν΅μ μ ꡬ쑰μ λ°μ΄ν° μ μ‘ λ°©μ (0) | 2024.12.12 |
docker-composeμ cmake (0) | 2024.11.09 |