MESSAGE
DATE | 2016-11-16 |
FROM | Ruben Safir
|
SUBJECT | Subject: [Learn] why use a reference wrapper int his example
|
From learn-bounces-at-nylxs.com Wed Nov 16 04:22:11 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 2DEFD161312; Wed, 16 Nov 2016 04:22:11 -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 D06E4160E77; Wed, 16 Nov 2016 04:22:07 -0500 (EST) To: Billy D , learn-at-nylxs.com From: Ruben Safir Message-ID: Date: Wed, 16 Nov 2016 04:22:07 -0500 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.4.0 MIME-Version: 1.0 Subject: [Learn] why use a reference wrapper int his example 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: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: learn-bounces-at-nylxs.com Sender: "Learn"
if anything, it would be the int I would think needs to be in a reference wrapper
http://en.cppreference.com/w/cpp/thread/unique_lock
#include #include #include
struct Box { explicit Box(int num) : num_things{num} {}
int num_things; std::mutex m; };
void transfer(Box &from, Box &to, int num) { // don't actually take the locks yet std::unique_lock lock1(from.m, std::defer_lock); std::unique_lock lock2(to.m, std::defer_lock);
// lock both unique_locks without deadlock std::lock(lock1, lock2);
from.num_things -= num; to.num_things += num;
// 'from.m' and 'to.m' mutexes unlocked in 'unique_lock' dtors }
int main() { Box acc1(100); Box acc2(50);
std::thread t1(transfer, std::ref(acc1), std::ref(acc2), 10); std::thread t2(transfer, std::ref(acc2), std::ref(acc1), 5);
t1.join(); t2.join(); }
-- So many immigrant groups have swept through our town that Brooklyn, like Atlantis, reaches mythological proportions in the mind of the world - RI Safir 1998 http://www.mrbrklyn.com
DRM is THEFT - We are the STAKEHOLDERS - RI Safir 2002 http://www.nylxs.com - Leadership Development in Free Software http://www2.mrbrklyn.com/resources - Unpublished Archive http://www.coinhangout.com - coins! http://www.brooklyn-living.com
Being so tracked is for FARM ANIMALS and and extermination camps, but incompatible with living as a free human being. -RI Safir 2013 _______________________________________________ Learn mailing list Learn-at-nylxs.com http://lists.mrbrklyn.com/mailman/listinfo/learn
|
|