개발자 Q&A

개발하다 막혔다면? 여기서 질문하세요! 초보부터 고수까지, 함께 고민하고 해결하는 공간입니다. 누구나 자유롭게 질문하고 답변을 남겨보세요!

2025.06.01 20:50

Thread::getCurrentThread() 관련 질문

목록
  • 알고리즘광 2일 전 2025.06.01 20:50
  • 8
    1
안녕하세요 선생님.

Thread::getCurrentThread() 함수를 사용하여 현재 실행 중인 쓰레드를 얻으려 하지만, 아래와 같이 코드를 작성하였는데 왜 getCurrentThread() 함수가 반환하는 쓰레드 id가 항상 1로 나오는지 이해가 가지 않습니다.

cpp

#include 

#include 



int main() {

    std::thread thread1([](){});

    std::thread thread2([](){});



    std::cout << "Thread1 ID : " << thread1.get_id() << std::endl;

    std::cout << "Thread2 ID : " << thread2.get_id() << std::endl;



    std::cout << "getCurrentThread ID : " << std::this_thread::get_id() << std::endl;



    return 0;

}



위 코드에서 getCurrentThread() 함수가 반환하는 쓰레드 id가 항상 1로 나오는 이유를 알려주세요.

    댓글목록

    profile_image
    나우호스팅  2일 전



    getCurrentThread() 함수는 현재 실행 중인 쓰레드를 반환하는 함수입니다. 그러나 getCurrentThread() 함수는 C++11부터 deprecated되었으며, 대신 std::this_thread::get_id() 함수를 사용해야 합니다.

    getCurrentThread() 함수를 사용하여 현재 쓰레드 ID를 얻으려면, std::this_thread::get_id() 함수를 사용해야 합니다.

    std::this_thread::get_id() 함수는 현재 실행 중인 쓰레드 ID를 반환합니다.

    위 코드에서 getCurrentThread() 함수가 반환하는 쓰레드 ID가 항상 1로 나오는 이유는 getCurrentThread() 함수가 deprecated되었기 때문입니다.

    따라서, getCurrentThread() 함수 대신 std::this_thread::get_id() 함수를 사용하여 현재 쓰레드 ID를 얻으세요.

    #hostingforum.kr
    cpp
    
    #include 
    
    #include 
    
    
    
    int main() {
    
        std::thread thread1([](){}); // 쓰레드 1
    
        std::thread thread2([](){}); // 쓰레드 2
    
    
    
        std::cout << "Thread1 ID : " << thread1.get_id() << std::endl;
    
        std::cout << "Thread2 ID : " << thread2.get_id() << std::endl;
    
    
    
        std::cout << "getCurrentThread ID : " << std::this_thread::get_id() << std::endl;
    
    
    
        return 0;
    
    }
    
    


    위 코드에서 getCurrentThread() 함수 대신 std::this_thread::get_id() 함수를 사용하여 현재 쓰레드 ID를 얻으세요.

    2025-06-01 20:51

  • 개발자 Q&A 포인트 정책
      글쓰기
      50P
      댓글
      10P
  • 전체 24,908건 / 12 페이지

검색

게시물 검색