C++ 멀티쓰레딩 (Multithreading)

2021. 3. 30. 00:03·SW개발/C++
반응형

멀티쓰레딩 (Multithreading)

  • <thread> 라이브러리

  • 병렬 프로그래밍을 위한 라이브러리이다.

  • 쓰레드를 생성하여 다른 코어에서 작업할 수 있도록 한다.


예제

  • CPU 사용률 100% 찍어보기

    • 출력이 섞여서 나오는 현상(레이스 컨디션)이 발생한다. 이는 mutex나 atomic, 세마포어 등으로 해결할 수 있다.
    #include <iostream>
    #include <string>
    #include <thread>
    #include <vector>
    
    int         main()
    {
        using namespace std;
    
        const int num_logical_processors = std::thread::hardware_concurrency();
    
        cout << "Number of processors : " << num_logical_processors << endl;
        cout << "ID of this thread : " << std::this_thread::get_id() << endl;
    
        vector<thread>  threads;
        threads.resize(num_logical_processors);
    
        for (auto& e : threads)
            e = thread([]() {
            cout << std::this_thread::get_id() << endl;
            while (true) {}});
    
        for (auto& e : threads)
            e.join();
    }
    
    /* stdout stderr
    Number of processors : 12
    ID of this thread : 16588
    13524
    14792
    17928
    18368
    17452
    14392
    19440
    12996
    16180
    1773614800
    18504
    */
반응형
저작자표시 (새창열림)

'SW개발 > C++' 카테고리의 다른 글

C++ future  (0) 2021.03.30
C++ 동시성 (Concurrency)  (0) 2021.03.30
C++ atomic  (0) 2021.03.30
C++ mutex  (0) 2021.03.30
C++ 람다 함수 (Lambda Function)  (0) 2021.03.30
'SW개발/C++' 카테고리의 다른 글
  • C++ future
  • C++ 동시성 (Concurrency)
  • C++ atomic
  • C++ mutex
Caniro
Caniro
  • Caniro
    Minimalism
    Caniro
  • 전체
    오늘
    어제
    • 전체보기 (319)
      • SW개발 (268)
        • Java Spring (6)
        • C++ (186)
        • Python (21)
        • Linux (16)
        • 알고리즘 (13)
        • Git (4)
        • Embedded (1)
        • Raspberrypi (9)
        • React (3)
        • Web (2)
        • Windows Device Driver (6)
      • IT(개발아님) (46)
        • Windows (26)
        • MacOS (7)
        • Utility (11)
      • 챗봇 짬통 (0)
      • 일상 (2)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    windows
    그림판
    Workspace
    스프링
    spring
    로지텍 마우스 제스처
    스프링 프레임워크 핵심 기술
    java
    SFC
    MacOS
    맥북 카카오톡 알림 안뜸
    알림
    vscode
    Solaris 10
    백기선
    Windows 11
    unix
    dism
    EXCLUDE
    logi options
    mspaint
    KakaoTalk
    제외
    윈도우 명령어
    SunOS 5.1
    윈도우
    시스템 복구
    citrix workspace
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
Caniro
C++ 멀티쓰레딩 (Multithreading)
상단으로

티스토리툴바