🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: Converting existing non-threaded app to threaded

#6191
From: Bob Gonder
Date: Wed, 01 Jun 2005 17:10
62 lines
3170 bytes
Matt Ross wrote:

>
>I modified my class TBaseObject to class TThread on both these classes, and 
>added all the requisite functions. I also modified the debug output function 
>which references back to the main class and the VCL to cope with shared 
>access. The way I intend to use them is have each thread start suspended and 
>use a timer on my main form to trigger the resuming of each thread. They go 
>off and do their thing, and suspend themselves when the work is done. The 
>next time the timer is activated it first checks the threads are all 
>suspended [i.e. they all finished doing their work last time round]. This is 
>the snag.

How about this.
Instead of using Resume, have the main thread set up 4 manual-reset
event 2 for animals, 2 for flood.
Each thread enters an outter loop the locks and incriments a static
counter...psudo code might be faster to explain....
Thread()
{	int	my_count;
	while( ! terminated )
	{	crit_lock()
		my_count = ++counter
		crit_unlock()
		if( my_count == TotalNumberOfAnimals )
		{
			ResetEvent( barn_empty );
			PostMessage( MainWnd, MSG_BarnFull )
		}
		waitforsingleobject( leave_barn, infinite);
		crit_lock()
		my_count = --counter
		crit_unlock()
		if( my_count == 0 )
		{
/* I am the last animal to leave the barn. let the other animals know
it is ok to come home now.*/
			ResetEvent( leave_barn );
			SetEvent( barn_empty );
		}
		if( termianted ) break;
		/* do the animal thing */
/* I might be fast and finish before everyone has started, so wait for
everyone to start */
		waitforsingleobject( barn_empty, infinite);
	}
	crit_lock()
	--counter;
	if( counter == 0 )
		ResetEvent( animal_event);
	crit_unlock()
}

When main receives MSG_BarnFull, then it could either enable the
timer, or just go ahead with the next round.

>The problem I'm having is that most of the time some of these threads never 
>make it back to the suspended state. Each time I run the application it 

If this is like the game of life, where each node is dependant upon
the state of it's neighbors, and you are locking access to neighbors
who are locking access to you...could be a deadlock.
Technical Details
Message-ID: <r5pr915usa5ffe44er8fif4un793sol0gc@4ax.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!news.glorb.com!news.moat.net!news.linkpendium.com!news.linkpendium.com!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail
References: <429d7aef$1@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,a95437282c3feb23
X-Google-Attributes: gid10cc67,public
X-Google-Language: ENGLISH,ASCII-7-bit
From: Bob Gonder <notbg@notmindspring.invalid>
Newsgroups: borland.public.cppbuilder.nativeapi
Subject: Re: Converting existing non-threaded app to threaded
Date: Wed, 01 Jun 2005 10:10:08 -0700
Organization: MindSpring Enterprises, Inc.
Reply-To: notbg@notmindspring.invalid
Message-ID: <r5pr915usa5ffe44er8fif4un793sol0gc@4ax.com>
References: <429d7aef$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: 67.77.154.209
X-Trace: newsgroups.borland.com 1117645806 67.77.154.209 (1 Jun 2005 10:10:06 -0700)
Lines: 64
Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!news.glorb.com!news.moat.net!news.linkpendium.com!news.linkpendium.com!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail