[Basic]Thread2 – detach, join

๐Ÿ‘‰๐Ÿป ์•„๋ž˜๋Š” ์Šค๋ ˆ๋“œ์—์„œ detach์™€ join ์— ๋Œ€ํ•œ ์„ค๋ช…์ž…๋‹ˆ๋‹ค.
Below is an explanation of detach and join in threads.

โœ”๏ธ detach

— detach๋ฐฉ์‹์€ ์Šค๋ ˆ๋“œ ๋™์ž‘์„ ๋ฐฑ๊ทธ๋ผ์šด๋“œ์—์„œ ์™„์ „ํžˆ ๋ถ„๋ฆฌ์‹œํ‚ต๋‹ˆ๋‹ค.
The detach method completely separates thread operations from the background.

— ์™„์ „ํžˆ ๋ถ„๋ฆฌ๋˜์–ด ๋…๋ฆฝ์ ์œผ๋กœ ๋™์ž‘ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ข…๋ฃŒ์‹œ์ ์„ ์•Œ๊ธฐ ์–ด๋ ต๊ณ  ์ œ์–ด๊ฐ€ ์–ด๋ ต์Šต๋‹ˆ๋‹ค.
Because they are completely separated and operate independently, it is difficult to know the termination point and difficult to control.

— ๋น„๊ต์  ๊ฐ„๋‹จํ•œ ์ž‘์—…์— ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
It is used for relatively simple tasks.

โœ”๏ธ join

— join()์€ joinable()์ด true์ด๋ฉด ์Šค๋ ˆ๋“œ๋ฅผ ๋Œ€๊ธฐ์ƒํƒœ๋กœ ๋งŒ๋“œ๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค.
join() is a function that puts a thread into a waiting state if joinable() is true.


— join์„ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ์‹์€ ์Šค๋ ˆ๋“œ์˜ ์ข…๋ฃŒ์ง€์ ์„ ๊ด€๋ฆฌ ๊ฐ€๋Šฅํ•˜๊ธฐ๋•Œ๋ฌธ์— ๋ณต์žกํ•œ ์„œ๋ฒ„๋‚˜ ๋Œ€๊ทœ๋ชจ ํ”„๋กœ๊ทธ๋žจ์—์„œ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.
The join method is used in complex servers or large-scale programs because it allows for the management of thread termination points.

โœ”๏ธ ์ฝ”๋“œ / Code

— detach.cpp

#include <iostream>
#include <thread>

void worker(int id) {
    std::cout << "Worker " << id << " started.\n";
    std::this_thread::sleep_for(std::chrono::seconds(1));
    std::cout << "Worker " << id << " finished.\n";
}

int main() {
    for(int i = 0; i < 3; ++i) {
        std::thread t(worker, i);

        // ์Šค๋ ˆ๋“œ๋ฅผ ๋ถ„๋ฆฌํ•˜์—ฌ ๋…๋ฆฝ ์‹คํ–‰
        // Separate threads for independent execution
        t.detach();  
    }
    
    std::cout << "Main thread finished without waiting.\n";
    // ๋ฉ”์ธ ์Šค๋ ˆ๋“œ๊ฐ€ ์—ฌ๊ธฐ์„œ ์ข…๋ฃŒ๋˜์–ด๋„ worker ์Šค๋ ˆ๋“œ๋Š” ๋ฐฑ๊ทธ๋ผ์šด๋“œ์—์„œ ๊ณ„์† ๋™์ž‘ํ•  ์ˆ˜ ์žˆ์Œ
    // Even if the main thread terminates here, the worker thread can continue to run in the background

    // ์Šค๋ ˆ๋“œ๊ฐ€ ์ž‘์—… ํ•  ์‹œ๊ฐ„ ํ™•๋ณด์šฉ(๋””๋ฒ„๊น… ๋ชฉ์ )
    // To secure time for the thread to work (for debugging purposes)
    std::this_thread::sleep_for(std::chrono::seconds(2)); 
    return 0;
}

— vt.cpp

#include <iostream>
#include <thread>
#include <vector>

void worker(int id) {
    std::cout << "Worker " << id << " started.\n";
    std::this_thread::sleep_for(std::chrono::seconds(1));
    std::cout << "Worker " << id << " finished.\n";
}

int main() {
    std::vector<std::thread> threads;

    for(int i = 0; i < 3; ++i) {
        std::thread t(worker, i);

        // ์Šค๋ ˆ๋“œ ๊ฐ์ฒด๋ฅผ ๋ฒกํ„ฐ์— ์ €์žฅ
        // Store thread objects in a vector
        threads.push_back(std::move(t));  
    }
    
    // ๋ชจ๋“  ์Šค๋ ˆ๋“œ๊ฐ€ ์ข…๋ฃŒ๋  ๋•Œ๊นŒ์ง€ ๊ธฐ๋‹ค๋ฆผ
    // Wait until all threads terminate
    for(auto &t : threads) {
        if(t.joinable()) {
            t.join();
        }
    }

    // ๋ฒกํ„ฐ์—์„œ ์Šค๋ ˆ๋“œ ๊ฐ์ฒด ์ œ๊ฑฐ
    // Remove thread objects from vector
    threads.clear(); 
    std::cout << "All workers completed.\n";
    return 0;
}

โœ”๏ธ ์ปดํŒŒ์ผ / Compiling

g++ vt.cpp -o vt -std=c++17 -pthread
g++ detach.cpp -o detach -std=c++17 -pthread

โœ”๏ธ์‹คํ–‰ / Run

./vt
./detach

Leave a Reply