🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.internet.socket
Article #1302

Re: Passing info between threads

#1302
From: Bob Gonder
Date: Tue, 20 Feb 2007 15:29
92 lines
3360 bytes
"Lesley Anne" <mspfila (dash) brl (at) yahoo (dot) com> wrote:

I'm a bit late, so I'll just throw out some comments

>       pLockSendString->Acquire();
>       pLockAssocID->Acquire();

You are releasing in the wrong order.
When using multiple locks, always (in all segments or your program)
lock them in the same order, and always release in the exact opposite
order.  This reduces the dreaded Deadlock.

>          pLockSendString->Release();
>          pLockAssocID->Release();

And then, it looks like you are always using Both locks, never a
single one, which makes one wonder why you need 2? A single lock
between the sender (VCL) and the receiver (thread) sould suffice.

Ok, one way to use 2:
(This is redundant and slow/inefficient [and wrong])

VCL:	LockOne()	// wait for Copy
	SetData()
	UnlockOne()	// allow Copy
	NotifyThread()	// wake up thread
	LockTwo()	// wait for thread to awaken
	UnlockTwo()	// allow thread to complete

Thread:	LockTwo()

ThreadNotify()
	LockOne()	// lock the data
	UnlockTwo()	// inform VCL we are awake
	CopyData()
	UnlockOne()	// allow changes to data
	LockTwo()	// wait for VCL to resume
	ProcessData()

No, there's still a race on Two.
I suppose one could attempt a Third, but it seems to be going in the
wrong direction.

There seems to be a concern with VCL being faster than Thread, and
overwritting or dropping data.
Single Lock:

VCL:
	LockData()	// wait for thread
	SetData()
	UnlockData()
	NotifyThread()

Thread:
	LockData()
	ProcessData()
	UnlockData()

Yes, there is still a race in that VCL could cycle through before
Thread gets going and locks the data.

 One way out is a simple sentinal.

VCL:
static volatile bool DataFull = false;

	while( DataFull ){ ProcessMessages() }
	SetData()
	DataFull = true
	ThreadSend( DataFull )

ThreadSend( bool & DataFull ):
	ProcessData()
	DataFull = false

VCL can cycle through, but will get stuck until Thread has finished.

>  (Or would it be OK to pass local 
>variables to the thread?  How would that work?)

You would need to wait for the thread to copy them.

VCL:
	SetData();
	ThreadSend( LocalData )
	WaitForSingleObject( hDataAquiredEvent, 10000 );
Thread:
	CopyData();
	Signal( hDataAquiredEvent );
	ProcessData()

(There is probably some TEvent that makes that easier)
Technical Details
Message-ID: <h21mt21bg7maqp25d2lqkr9b1iet0eu3g6@4ax.com>
Path: arc1-ca.pugleaf.net!g2news2.google.com!news1.google.com!news4.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail
References: <45d9e83a$1@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 11508e,234a9a04e64155b0
X-Google-Attributes: gid11508e,public
X-Google-Language: ENGLISH,ASCII-7-bit
From: Bob Gonder <notbg@notmindspring.invalid>
Newsgroups: borland.public.cppbuilder.internet.socket
Subject: Re: Passing info between threads
Date: Tue, 20 Feb 2007 07:29:22 -0800
Organization: MindSpring Enterprises, Inc.
Reply-To: notbg@notmindspring.invalid
Message-ID: <h21mt21bg7maqp25d2lqkr9b1iet0eu3g6@4ax.com>
References: <45d9e83a$1@newsgroups.borland.com>
X-Newsreader: Forte Agent 1.93/32.576 English (American)
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
NNTP-Posting-Host: 69.69.88.225
X-Trace: newsgroups.borland.com 1171985323 69.69.88.225 (20 Feb 2007 07:28:43 -0800)
Lines: 96
Path: arc1-ca.pugleaf.net!g2news2.google.com!news1.google.com!news4.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail