std::thread:: thread
|
thread
(
)
noexcept
;
|
(1) | (C++11 이후) |
|
thread
(
thread
&&
other
)
noexcept
;
|
(2) | (C++11 이후) |
|
template
<
class
F,
class
...
Args
>
explicit thread ( F && f, Args && ... args ) ; |
(3) | (C++11 이후) |
|
thread
(
const
thread
&
)
=
delete
;
|
(4) | (C++11 이후) |
새로운
std::thread
객체를 생성합니다.
std::thread
객체를 생성합니다.
std::thread
객체를
other
가 나타내던 실행 스레드를 나타내도록 생성합니다. 이 호출 이후
other
는 더 이상 실행 스레드를 나타내지 않습니다.
std::thread
객체를 생성하고 실행 스레드와 연결합니다. 새로운 실행 스레드는 다음을 실행하기 시작합니다:
|
INVOKE
(
decay-copy
(
std::
forward
<
F
>
(
f
)
)
,
|
(C++23 이전) |
|
std::
invoke
(
auto
(
std::
forward
<
F
>
(
f
)
)
,
|
(C++23 이후) |
std::thread
와 동일한 타입이 아닌 경우.
|
다음 조건 중 하나라도 만족되면 프로그램은 ill-formed입니다:
|
(C++20 이전) |
|
다음 중 하나라도 false 이면 프로그램은 ill-formed입니다:
|
(C++20 이후) |
std::thread
객체가 동일한 실행 스레드를 나타낼 수 없습니다.
목차 |
매개변수
| other | - | 이 스레드 객체를 생성하기 위한 다른 스레드 객체 |
| f | - | Callable 새 스레드에서 실행할 호출 가능 객체 |
| args | - | 새 함수에 전달할 인수들 |
사후 조건
예외
std::errc::resource_unavailable_try_again
오류 조건이나 다른 구현별 오류 조건을 나타낼 수 있습니다.
참고 사항
스레드 함수에 전달되는 인수들은 값에 의해 이동되거나 복사됩니다. 참조 인수를 스레드 함수에 전달해야 하는 경우, 해당 인수를 감싸야 합니다(예: std::ref 또는 std::cref 사용).
함수에서 반환되는 모든 값은 무시됩니다. 함수가 예외를 발생시키면, std::terminate 가 호출됩니다. 반환값이나 예외를 호출 스레드로 전달하기 위해서는, std::promise 또는 std::async 를 사용할 수 있습니다.
예제
#include <chrono> #include <iostream> #include <thread> #include <utility> void f1(int n) { for (int i = 0; i < 5; ++i) { std::cout << "Thread 1 executing\n"; ++n; std::this_thread::sleep_for(std::chrono::milliseconds(10)); } } void f2(int& n) { for (int i = 0; i < 5; ++i) { std::cout << "Thread 2 executing\n"; ++n; std::this_thread::sleep_for(std::chrono::milliseconds(10)); } } class foo { public: void bar() { for (int i = 0; i < 5; ++i) { std::cout << "Thread 3 executing\n"; ++n; std::this_thread::sleep_for(std::chrono::milliseconds(10)); } } int n = 0; }; class baz { public: void operator()() { for (int i = 0; i < 5; ++i) { std::cout << "Thread 4 executing\n"; ++n; std::this_thread::sleep_for(std::chrono::milliseconds(10)); } } int n = 0; }; int main() { int n = 0; foo f; baz b; std::thread t1; // t1은 스레드가 아님 std::thread t2(f1, n + 1); // 값으로 전달 std::thread t3(f2, std::ref(n)); // 참조로 전달 std::thread t4(std::move(t3)); // t4가 이제 f2()를 실행 중. t3은 더 이상 스레드가 아님 std::thread t5(&foo::bar, &f); // t5는 객체 f에서 foo::bar()를 실행 std::thread t6(b); // t6는 객체 b의 복사본에서 baz::operator()를 실행 t2.join(); t4.join(); t5.join(); t6.join(); std::cout << "Final value of n is " << n << '\n'; std::cout << "Final value of f.n (foo::n) is " << f.n << '\n'; std::cout << "Final value of b.n (baz::n) is " << b.n << '\n'; }
가능한 출력:
Thread 1 executing Thread 2 executing Thread 3 executing Thread 4 executing Thread 3 executing Thread 1 executing Thread 2 executing Thread 4 executing Thread 2 executing Thread 3 executing Thread 1 executing Thread 4 executing Thread 3 executing Thread 2 executing Thread 1 executing Thread 4 executing Thread 3 executing Thread 1 executing Thread 2 executing Thread 4 executing Final value of n is 5 Final value of f.n (foo::n) is 5 Final value of b.n (baz::n) is 0
결함 보고서
다음의 동작 변경 결함 보고서들은 이전에 발표된 C++ 표준에 소급 적용되었습니다.
| DR | 적용 대상 | 게시된 동작 | 올바른 동작 |
|---|---|---|---|
| LWG 2097 | C++11 |
오버로드
(3)
에서
F
가
std::thread
일 수 있었음
|
F
에 제약이 추가됨
|
| LWG 3476 | C++20 |
오버로드
(3)
이 (decayed 타입의)
F
와 인수 타입들이 이동 생성 가능할 것을 직접 요구함
|
이러한 요구사항들을
제거함 [1] |
- ↑ 이동 생성 가능성은 이미 간접적으로 std::is_constructible_v 에 의해 요구됩니다.
참고문헌
- C++23 표준 (ISO/IEC 14882:2024):
-
- 33.4.3.3 스레드 생성자 [thread.thread.constr]
- C++20 표준(ISO/IEC 14882:2020):
-
- 32.4.2.2 스레드 생성자 [thread.thread.constr]
- C++17 표준 (ISO/IEC 14882:2017):
-
- 33.3.2.2 thread 생성자 [thread.thread.constr]
- C++14 표준(ISO/IEC 14882:2014):
-
- 30.3.1.2 thread 생성자 [thread.thread.constr]
- C++11 표준 (ISO/IEC 14882:2011):
-
- 30.3.1.2 thread 생성자 [thread.thread.constr]
참고 항목
새로운
jthread
객체를 생성함
(
std::jthread
의 public 멤버 함수)
|
|
|
C 문서
for
thrd_create
|
|