일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- 포토샵
- dataframe
- grid
- Prefix Sums
- Design Pattern
- c
- 강화학습
- skt membership
- 소수
- stl
- Javascript
- 상태
- CSS
- c++
- SK바이오사이언스
- 백준
- Flexbox
- 알고리즘
- align-items
- 통신사할인
- Gap
- 확률
- Photoshop
- series
- 에라토스테네스의 체
- Codility
- spring
- pandas
- margin
- 수학
Archives
- Today
- Total
sliver__
[Mastering C++ Programming] - How to write a multithreaded application using the native C++ thread feature 본문
CS/C++
[Mastering C++ Programming] - How to write a multithreaded application using the native C++ thread feature
sliver__ 2022. 12. 8. 22:37728x90
- C++ 스레드 지원 라이브러리를 사용하여 다중 스레드 애플리케이션을 작성하는 것은 매우 간단합니다.
- 스레드 클래스는 C++11에서 도입되었습니다.
- 이 함수는 스레드를 생성하는 데 사용할 수 있습니다.
- 이 함수와 동등한 기능은 POSIX pthread 라이브러리의 pthread_create입니다.
#include <thread>
using namespace std;
thread instance ( thread_procedure )
- 인자는 아래와 같습니다
- 이제 다음 코드에서 스레드 ID를 반환하는 인수는 아래와 같습니다.
this_thread::get_id ()
- POSIX thread의 pthread_self() 와 같습니다.
thread::join()
- join() 함수는 호출자 스레드 또는 기본 스레드를 차단하는 데 사용되므로 조인한 스레드가 작업을 완료할 때까지 대기합니다.
- 이것은 non-static 함수이므로 스레드 개체에서 호출해야 합니다.
- 예제는 아래와 같습니다.
#include <thread>
#include <iostream>
using namespace std;
void threadProc() {
for( int count=0; count<3; ++count ) {
cout << "Message => "
<< count
<< " from "
<< this_thread::get_id()
<< endl;
}
}
int main() {
thread thread1 ( threadProc );
thread thread2 ( threadProc );
thread thread3 ( threadProc );
thread1.join();
thread2.join();
thread3.join();
return 0;
}
g++ main.cpp -std=c++17 -lpthread
728x90
'CS > C++' 카테고리의 다른 글
[Mastering C++ Programming] - Mutex (0) | 2022.12.08 |
---|---|
[Mastering C++ Programming] - Thread Synchronization (0) | 2022.12.08 |
[Mastering C++ Programming] - Creating threads with the pthreads library (0) | 2022.12.08 |
[Mastering C++ Programming] - weak_ptr / Circular dependency (0) | 2022.12.07 |
[Mastering C++ Programming] - shared_ptr (0) | 2022.12.07 |
Comments