🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.nativeapi
Article #337

Re: Conceptual design question...

#337
From: Bob Gonder
Date: Fri, 02 May 2008 15:15
118 lines
4344 bytes
Mike Collins wrote:

>thanks for the reply - can you expand a bit - what do you mean by a memory 
>sentinal - are you just taking about some sort of global variable that 
>indicates that the messages were sent internally?

Yes.

> What is the situation with ensuring thread access safety?

Minor issue with polling (see last paragraph).
No issue with messages (in your case).

You would want to have one variable per thread
so each thread can be sure that you really
called it, and not one of the others.

The variable could be global or thread:

If the main thread is calling, then it has
access to the TThreads and can write to a
public bool that you add to the class.

class MyThread	public TThread
{
public
	bool	Sentinal;
	/* other stuff */
	/* don't forget to construct Sentinal */
}
	MyThread *MyThreads[4];

   case LOGIN:
	for( int i=0; i<4; ++i )
	{
		MyThreads[ i ]->Sentinal = true;
		Signal( MyThreads[ i ] );
	};

Or a global Sentinal[4] where each thread
knows it's number (can be passed via
CreateThread lpParameter).

	bool	Sentinal[4] = {false};
	TThread *MyThreads[4];

   case LOGIN:
	for( int i=0; i<4; ++i )
	{
		Sentinal[ i ] = true;
		Signal( MyThreads[ i ] );
	};

The thread then gets the message and checks
this->Sentinal. If true, set to false and process.
If false, go back to sleep.

	case MySignal:
		if( Sentinal )
		{	Sentinal = false; //minimize race
			ProcessLogin();
		};
		break;
 - or -
	case MySignal:
		if( Sentinal[ MyNumber ] )
		{	Sentinal[ MyNumber ] = false; //minimize race
			ProcessLogin();
		};
		break;

The only issue is if main can try to signal the
same thread before the thread has checked/reset
the flag. There could be a race on the reset-set
and if the race looses, the thread ignores
the second legit message.

Seems doubtfull that user logins would happen
fast enough to cause a race.

In other situations, where you might get
race conditions, you would want to replace
the bool with an int and do locked incriment/decriment.
	InterlockedIncrement()
	InterlockedDecrement()


  -- Cascade Messages
And, finally, there is cascade.
Main sets global flag.
Main wakes up thread 1.
Thread 1 ensures legit wakeup and wakes up thread 2.
Thread 2 ensures legit wakeup and wakes up thread 3.
Thread 3 ensures legit wakeup and wakes up thread 4.
Thread 4 ensures legit wakeup and resets global flag.

This has a much larger race window, but serializes
the processing.


  -- Polling Race
If you decide to add a sentinal check to your polling....

Main writes "true" whenever it wants.
Thread is only reading (polling).

If there is a race, the thread will read either true or false.
If false, then it hits next cycle, otherwise it hits this time.

Main will finish the write before it can be swapped out.
The thread should therefore only need to wait a few
instructions (multi-cpu only, single-cpu is a non-issue)
before it is safe to reset the bool. The testing of
the bool after reading it should be adequate delay.

Overflow occures only if main cycles "true" faster
than the threads can poll. If that can happen,
then you switch to a locked counter.
Technical Details
Message-ID: <fg5m14dofuofnv6dusrkk6o2jrecjkfflj@4ax.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!news.isoc.lu!newsgroups.borland.com!not-for-mail
References: <4817b8f3@newsgroups.borland.com> <hlvg145v5292euvns2sg6klblk4sgjv9bq@4ax.com> <8nrj14576vnl3cf62pgbgcgvc4a7vho0fg@4ax.com> <481a729b@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,7f6e6520c419b9ba
X-Google-Attributes: gid10cc67,domainid0,public,usenet
X-Google-Language: ENGLISH,ASCII-7-bit
From: Bob Gonder <notbg@notmindspring.invalid>
Newsgroups: borland.public.cppbuilder.nativeapi
Subject: Re: Conceptual design question...
Date: Fri, 02 May 2008 08:15:10 -0700
Organization: MindSpring Enterprises, Inc.
Reply-To: notbg@notmindspring.invalid
Message-ID: <fg5m14dofuofnv6dusrkk6o2jrecjkfflj@4ax.com>
References: <4817b8f3@newsgroups.borland.com> <hlvg145v5292euvns2sg6klblk4sgjv9bq@4ax.com> <8nrj14576vnl3cf62pgbgcgvc4a7vho0fg@4ax.com> <481a729b@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: 67.77.144.65
X-Trace: newsgroups.borland.com 1209741308 67.77.144.65 (2 May 2008 08:15:08 -0700)
Lines: 121
Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!news.isoc.lu!newsgroups.borland.com!not-for-mail