C++ 멀티쓰레딩 (Multithreading)

2021. 3. 30. 00:03·C++/Library

멀티쓰레딩 (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
    */
저작자표시 (새창열림)

'C++ > Library' 카테고리의 다른 글

C++ forward  (0) 2021.03.30
C++ future  (0) 2021.03.30
C++ atomic  (0) 2021.03.30
C++ mutex  (0) 2021.03.30
C++ 파일 임의 위치 접근  (0) 2021.03.29
'C++/Library' 카테고리의 다른 글
  • C++ forward
  • C++ future
  • C++ atomic
  • C++ mutex
Caniro
Caniro
  • Caniro
    Minimalism
    Caniro
  • 전체
    오늘
    어제
    • 분류 전체보기 (317)
      • Algorithm (13)
        • 알기 쉬운 알고리즘 (10)
        • Search (1)
        • Sort (2)
      • Arduino (0)
      • C++ (185)
        • Class (46)
        • Exception (6)
        • Library (51)
        • Overloading (10)
        • SmartPointer (5)
        • Syntax (33)
        • TBC++ (23)
        • Templates (9)
        • VisualStudio (2)
      • Embedded (1)
      • Git (4)
      • Java (5)
      • Linux (16)
        • Error (1)
        • Linux Structure (11)
      • MacOS (7)
      • OS (1)
        • Concurrency (1)
      • Python (21)
        • Class (1)
        • Function (2)
        • Syntax (17)
      • Raspberrypi (9)
      • Review (1)
      • Utility (12)
        • VSCode (5)
        • VirtualBox (3)
      • Web (8)
        • Nginx (1)
        • React (3)
        • Django (1)
      • Windows (20)
        • Registry (3)
        • WSL (1)
        • DeviceDriver (6)
  • 블로그 메뉴

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

  • 공지사항

  • 인기 글

  • 태그

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

  • 최근 글

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

티스토리툴바