Details in the process of constructing a std::thread object

只愿长相守 提交于 2019-12-01 08:36:25

1) This "thread-accessible storage" bit of text is not represented directly in the standard. The standard simply says that the function is invoked with arguments obtained by decay_copy.

2) If you study decay_copy closely, you will find that it returns by value (because its return type is std::decay of something). So the function f is called with rvalue arguments (prvalue arguments, in fact).

If you want to pass lvalues (references), you can use std::ref and std::cref to wrap them.

The exact quote, C++11 30.3.1.2/4:

Effects: Constructs an object of type thread. The new thread of execution executes INVOKE(DECAY_COPY ( std::forward<F>(f)), DECAY_COPY (std::forward<Args>(args))...) with the calls to DECAY_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 invocation of INVOKE(DECAY_COPY ( std::forward<F>(f)), DECAY_COPY (std::forward<Args>(args))...) terminates with an uncaught exception, std::terminate shall be called.

DECAY_COPY is defined in 30.2.6/1:

In several places in this Clause the operation DECAY_COPY(x) is used. All such uses mean call the function decay_copy(x) and use the result, where decay_copy is defined as follows:

template <class T> typename decay<T>::type decay_copy(T&& v)
{ return std::forward<T>(v); }

INVOKE is defined in 20.8.2 pretty much in the same way as cppreference describes the invocation in the link you've provided.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!