[thread.jthread.cons] (original) (raw)

32 Thread support library [thread]

32.4 Threads [thread.threads]

32.4.3 Class jthread [thread.jthread.class]

32.4.3.1 Constructors, move, and assignment [thread.jthread.cons]

Effects:Constructs a jthread object that does not represent a thread of execution.

Postconditions: get_­id() == id() is trueand ssource.stop_­possible() is false.

template<class F, class... Args> explicit jthread(F&& f, Args&&... args);

Constraints: remove_­cvref_­t<F> is not the same type as jthread.

Mandates:The following are all true:

Preconditions: decay_­t<F> and each type in decay_­t<Args> meet theCpp17MoveConstructible requirements.

Effects:Initializes ssource.

The new thread of execution executes

invoke(decay-copy(std::forward(f)), get_stop_token(), decay-copy(std::forward(args))...)

if that expression is well-formed, otherwise

invoke(decay-copy(std::forward(f)), decay-copy(std::forward(args))...)

with the calls todecay-copy being evaluated in the constructing thread.

Any return value from this invocation is ignored.

[ Note

:

This implies that any exceptions not thrown from the invocation of the copy of f will be thrown in the constructing thread, not the new thread.

end note

]

If the invoke expression exits via an exception,terminate is called.

Synchronization:The completion of the invocation of the constructor synchronizes with the beginning of the invocation of the copy of f.

Postconditions: get_­id() != id() is trueand ssource.stop_­possible() is trueand *this represents the newly started thread.

[ Note

:

The calling thread can make a stop request only once, because it cannot replace this stop token.

end note

]

Throws: system_­error if unable to start the new thread.

Error conditions:

jthread(jthread&& x) noexcept;

Postconditions: x.get_­id() == id()and get_­id() returns the value of x.get_­id()prior to the start of construction.

ssource has the value of x.ssourceprior to the start of construction and x.ssource.stop_­possible() is false.

Effects:If joinable() is true, calls request_­stop() and then join().

[ Note

:

Operations on *this are not synchronized.

end note

]

jthread& operator=(jthread&& x) noexcept;

Effects:If joinable() is true, calls request_­stop() and then join().

Assigns the state of x to *thisand sets x to a default constructed state.

Postconditions: x.get_­id() == id()and get_­id() returns the value of x.get_­id()prior to the assignment.

ssource has the value of x.ssourceprior to the assignment and x.ssource.stop_­possible() is false.