On 11/19/2016 12:58 PM, Alf P. Steinbach wrote:
>> std::thread t(update_data_for_widget, w, std::ref(data) );
>>
>> Why can't you just send a reference?
>
> Because these arguments are not passed on directly to the thread
> function: they're stored by value, and passed on later.
Reminds me of a similar question I had:
http://thread.gmane.org/gmane.comp.gcc.help/49709
see also:
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=68784
I was also surprised; in principle the language semantics could allow
using a reference type, but this is intentionally disabled by the
standard for std::thread - you have to explicitly use std::ref().
Apparently this was motivated by 'helping' the programmer not pass data
by reference accidentally between threads.
I am not sure I like this approach, though. I think that it is a basic
knowledge of any skilled C++ programmer to know how to use references.
What I like of C++ is its solid foundation of self-consistent language
logic. Introducing this type of exception to the basic semantics is not
really attractive to me.