MESSAGE
DATE | 2016-11-19 |
FROM | ruben safir
|
SUBJECT | Subject: [Learn] Fwd: ref use
|
From learn-bounces-at-nylxs.com Sat Nov 19 11:49:44 2016 Return-Path: X-Original-To: archive-at-mrbrklyn.com Delivered-To: archive-at-mrbrklyn.com Received: from www.mrbrklyn.com (www.mrbrklyn.com [96.57.23.82]) by mrbrklyn.com (Postfix) with ESMTP id 16CFB161314; Sat, 19 Nov 2016 11:49:44 -0500 (EST) X-Original-To: learn-at-nylxs.com Delivered-To: learn-at-nylxs.com Received: from [10.0.0.62] (flatbush.mrbrklyn.com [10.0.0.62]) by mrbrklyn.com (Postfix) with ESMTP id 57524161312 for ; Sat, 19 Nov 2016 11:49:41 -0500 (EST) References: To: learn-at-nylxs.com From: ruben safir X-Forwarded-Message-Id: Message-ID: <1c5cfbc0-b8c7-f410-5415-93b9d702b516-at-mrbrklyn.com> Date: Sat, 19 Nov 2016 11:49:40 -0500 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.4.0 MIME-Version: 1.0 In-Reply-To: Content-Type: multipart/mixed; boundary="------------AF14BDA115BB3B24068FA396" Subject: [Learn] Fwd: ref use X-BeenThere: learn-at-nylxs.com X-Mailman-Version: 2.1.17 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: learn-bounces-at-nylxs.com Sender: "Learn"
This is a multi-part message in MIME format. --------------AF14BDA115BB3B24068FA396 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit
--------------AF14BDA115BB3B24068FA396 Content-Type: message/rfc822; name="ref use.eml" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="ref use.eml"
Path: reader2.panix.com!panix!not-for-mail From: Popping mad Newsgroups: comp.lang.c++ Subject: ref use Date: Wed, 16 Nov 2016 10:38:09 +0000 (UTC) Organization: PANIX Public Access Internet and UNIX, NYC Message-ID: NNTP-Posting-Host: www.mrbrklyn.com Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Trace: reader2.panix.com 1479292689 27290 96.57.23.82 (16 Nov 2016 10:38:09 GMT) X-Complaints-To: abuse-at-panix.com NNTP-Posting-Date: Wed, 16 Nov 2016 10:38:09 +0000 (UTC) To: learn-at-nylxs.com User-Agent: Pan/0.140 (Chocolate Salty Balls; GIT b8fc14e git.gnome.org/git/pan2) Xref: panix comp.lang.c++:1125257
I'm reading up on concurrency with the thread libraryis C++ Concurrency in action by Anthony Williams and I'm puzzled by one example having to do with sending references of objects through the threads. If you own the text, it is one page 24 where he is callable functions is
void update_data_for_widget(widget_id w, widget_data& data);
the call for the thread is
std::thread t (update_data_for_widget, w, data); where data is an object, of data I suppose ;)
to do this correctly he says we need to use ref and a reference wrapper
std::thread t(update_data_for_widget, w, std::ref(data) );
Why can't you just send a reference?
std::thread t(update_data_for_widget, w, &data );
--------------AF14BDA115BB3B24068FA396 Content-Type: message/rfc822; name="Re: ref use.eml" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="Re: ref use.eml"
Path: reader2.panix.com!panix!not-for-mail From: ruben safir Newsgroups: comp.lang.c++ Subject: Re: ref use Date: Fri, 18 Nov 2016 21:13:03 -0500 Organization: PANIX Public Access Internet and UNIX, NYC Message-ID: References: NNTP-Posting-Host: www.mrbrklyn.com Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit X-Trace: reader2.panix.com 1479521583 13857 96.57.23.82 (19 Nov 2016 02:13:03 GMT) X-Complaints-To: abuse-at-panix.com NNTP-Posting-Date: Sat, 19 Nov 2016 02:13:03 +0000 (UTC) User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.4.0 In-Reply-To: Xref: panix comp.lang.c++:1125335
On 11/16/2016 05:38 AM, Popping mad wrote: > I'm reading up on concurrency with the thread libraryis C++ Concurrency > in action by Anthony Williams and I'm puzzled by one example having to do > with sending references of objects through the threads. If you own the > text, it is one page 24 where he is callable functions is > > void update_data_for_widget(widget_id w, widget_data& data); > > the call for the thread is > > std::thread t (update_data_for_widget, w, data); where data is an object, > of data I suppose ;) > > to do this correctly he says we need to use ref and a reference wrapper > > std::thread t(update_data_for_widget, w, std::ref(data) ); > > Why can't you just send a reference? > > std::thread t(update_data_for_widget, w, &data ); >
nadah on this?
--------------AF14BDA115BB3B24068FA396 Content-Type: message/rfc822; name="Re: ref use.eml" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="Re: ref use.eml"
Path: reader2.panix.com!panix!goblin2!goblin1!goblin.stu.neva.ru!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: "Alf P. Steinbach" Newsgroups: comp.lang.c++ Subject: Re: ref use Date: Sat, 19 Nov 2016 12:58:40 +0100 Organization: A noiseless patient Spider Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Sat, 19 Nov 2016 12:00:44 -0000 (UTC) Injection-Info: mx02.eternal-september.org; posting-host="f7748555ce7a02b092ea3222aaba1336"; logging-data="11219"; mail-complaints-to="abuse-at-eternal-september.org"; posting-account="U2FsdGVkX18Q4FeNIVwA4bsh9ES1oerG" User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:45.0) Gecko/20100101 Thunderbird/45.5.0 In-Reply-To: Cancel-Lock: sha1:EJmsTQxjimn8MGmQg8hLIwa2PZo= Xref: panix comp.lang.c++:1125339
On 16.11.2016 11:38, Popping mad wrote: > I'm reading up on concurrency with the thread libraryis C++ Concurrency > in action by Anthony Williams and I'm puzzled by one example having to do > with sending references of objects through the threads. If you own the > text, it is one page 24 where he is callable functions is > > void update_data_for_widget(widget_id w, widget_data& data); > > the call for the thread is > > std::thread t (update_data_for_widget, w, data); where data is an object, > of data I suppose ;) > > to do this correctly he says we need to use ref and a reference wrapper > > 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.
> std::thread t(update_data_for_widget, w, &data );
This is not a reference, it's a pointer.
A pointer would work fine, it can be stored by value, but then the thread function signature needs to be adjusted accordingly.
Cheers & hth.,
- Alf
--------------AF14BDA115BB3B24068FA396 Content-Type: message/rfc822; name="Re: ref use.eml" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="Re: ref use.eml"
Path: reader2.panix.com!panix!goblin3!goblin1!goblin2!goblin.stu.neva.ru!aioe.org!.POSTED!not-for-mail From: Manfred Newsgroups: comp.lang.c++ Subject: Re: ref use Date: Sat, 19 Nov 2016 16:59:46 +0100 Organization: Aioe.org NNTP Server Message-ID: References: NNTP-Posting-Host: Oxw+WWYQkT5mdTlKqxXz7g.user.gioia.aioe.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Complaints-To: abuse-at-aioe.org User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:45.0) Gecko/20100101 Thunderbird/45.4.0 X-Notice: Filtered by postfilter v. 0.8.2 Xref: panix comp.lang.c++:1125340
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.
--------------AF14BDA115BB3B24068FA396 Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline
_______________________________________________ Learn mailing list Learn-at-nylxs.com http://lists.mrbrklyn.com/mailman/listinfo/learn
--------------AF14BDA115BB3B24068FA396--
|
|