MESSAGE
DATE | 2016-12-21 |
FROM | Ruben Safir
|
SUBJECT | Subject: [Learn] (fwd) Re: thread concurancy
|
From learn-bounces-at-nylxs.com Wed Dec 21 17:19:43 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 029E4161315; Wed, 21 Dec 2016 17:19:42 -0500 (EST) X-Original-To: learn-at-nylxs.com Delivered-To: learn-at-nylxs.com Received: from mailbackend.panix.com (mailbackend.panix.com [166.84.1.89]) by mrbrklyn.com (Postfix) with ESMTP id 1043C161311 for ; Wed, 21 Dec 2016 17:19:40 -0500 (EST) Received: from panix3.panix.com (panix3.panix.com [166.84.1.3]) by mailbackend.panix.com (Postfix) with ESMTP id D2A0C13EAF for ; Wed, 21 Dec 2016 17:19:39 -0500 (EST) Received: by panix3.panix.com (Postfix, from userid 20529) id AFAA72EB51; Wed, 21 Dec 2016 17:19:39 -0500 (EST) From: Ruben Safir To: learn-at-nylxs.com User-Agent: tin/2.2.1-20140504 ("Tober an Righ") (UNIX) (NetBSD/6.1.5 (i386)) Message-Id: <20161221221939.AFAA72EB51-at-panix3.panix.com> Date: Wed, 21 Dec 2016 17:19:39 -0500 (EST) Subject: [Learn] (fwd) Re: thread concurancy 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: , MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: learn-bounces-at-nylxs.com Sender: "Learn"
-- forwarded message -- Path: reader1.panix.com!panix!goblin1!goblin.stu.neva.ru!border1.nntp.ams1.giganews.com!border2.nntp.ams1.giganews.com!nntp.giganews.com!buffer2.nntp.ams1.giganews.com!news.giganews.com.POSTED!not-for-mail NNTP-Posting-Date: Wed, 21 Dec 2016 15:39:58 -0600 Date: Wed, 21 Dec 2016 23:39:57 +0200 From: Paavo Helde User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:25.8) Gecko/20151117 FossaMail/25.1.9 MIME-Version: 1.0 Newsgroups: comp.lang.c++ Subject: Re: thread concurancy References: In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Message-ID: Lines: 87 X-Usenet-Provider: http://www.giganews.com X-Trace: sv3-fShAD/U4DO8KJN3+4fcXmGn9Iwhlnr/QCB3wOlbkJxYmNnLZzhXJr/xX8e+G1GD6WhGTqPMKMd/t5bD!rOB6T9SyxN7aOSI2kHIHLPdYWJtKKb5nJUUhtm1nJvR7LpLejywekEtik2fhr1UtzCQugNuBlA4W X-Complaints-To: abuse-at-giganews.com X-DMCA-Notifications: http://www.giganews.com/info/dmca.html X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.40 X-Original-Bytes: 3875 Xref: panix comp.lang.c++:1126201
On 21.12.2016 22:48, Popping mad wrote: > :( > > I don't know. I'm very confused about the behavior of this test program I've been right. > I'm trying to move date by worker threads from one blob of memory to another and the debugger > is saying that the pointers beg and canvas_index is jumping 10 bytes between this two lines > > for(int i = 0; i < 10;i++) > { > t[i] = std::thread([this]{ readin(beg, canvas_index); }); > > and I'm not sure why. I lost the concurrency somewhere, but can't seem to figure out what I did wrong
Who knows. Debuggers sometimes also get confused and display wrong data.
Your example is incomplete (truncated?), cannot be compiled and even the types of beg and canvas_index are not known. So not much can be said about the code.
From the visible code I can only infer that you have not understood why and how to protect data with mutexes (mutex lock is in one thread only, and the data apparently protected by the lock (beg, canvas_index) is not even accessed in the other threads.
Also, access to the shared data buffer (canvas) is creating a lot of false sharing as the slicing size 10 is most probably not divisible by the cache line size, thus causing potentially significant performance penalties (probably not important for your toy example, but worth to mention).
> > #include > #include > #include > std::mutex medco; > std::mutex master; > > namespace testing{ > std::thread t[10]; > class PIC > { > public: > PIC():beg{&source[0]} > { > canvas_index = canvas; > std::cout << "Testing Source" << std::endl; > for(int i = 0; i<100; i++) > { > std::cout << i << " " << source[i] << std::endl ; > } > for(int i = 0; i < 10;i++) > { > t[i] = std::thread([this]{ readin(beg, canvas_index); }); > std::cerr << i << ": Making a thread" << std::endl; > sync_canvas_and_input(); > } > }; > > void sync_canvas_and_input() > { > std::cout << "**LOCKING**" << std::endl; > std::lock_guard turn(medco); > beg += 10; > canvas_index += 10; > } > > ~PIC() > { > std::cerr << "In the destructor" << std::endl; > for(int i=0; i<10; i++) > { > t[i].join(); > std::cerr << i << ": Joining a thread" << std::endl; > } > > }; > void readin(char * start, char * loc_canvas_index) > { > for( int i = 9; i>=0; i-- ) > { > *loc_canvas_index = start[i]; > std::cerr << i << ": Copy " << start[i] << std::endl; > std::cerr << i << ": Copied to loc_canvas_index " << reinterpret_cast(*loc_canvas_index) << std::endl; > loc_canvas_index++; > } >
-- end of forwarded message -- _______________________________________________ Learn mailing list Learn-at-nylxs.com http://lists.mrbrklyn.com/mailman/listinfo/learn
|
|