🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: Converting existing non-threaded app to threaded

#6198
From: Matt Ross
Date: Thu, 02 Jun 2005 08:40
63 lines
4000 bytes
"Oliver Rutsch" <orutsch@sympatec.com> wrote in message 
news:429dd4f1@newsgroups.borland.com...
> Hi, Matt.
>
>> I have a fairly large C++ Builder 6 project I would like to make 
>> threaded. Apart from the main class which handles most of the UI, I have 
>> two other major C++ classes that are just about pure C++ with no VCL. I 
>> would like to convert these to threaded classes. This is a simulation of 
>> artificial life. One of the classes is instantiated anything up to one 
>> hundred times [the living animals] and the other up to three or four 
>> hundred [the food]. This would mean I would have upwards of 500 threads, 
>> is this possibly under Windows XP?
>>
>
> 500? I think it is possible but it makes no sense. Why must every object 
> have it's own thread? Why not only a few (e.g. num of CPU's) threads who 
> manage theses objects? Too many threads will loose a lot of time with OS 
> switching between the threads.

This is a conversion of an existing application which has many instances of 
these two classes. As they have quite a bit of code in each to handle 
behaviour I could not think of any other logical way of threading each so 
they run independently of each other.

>> 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.
>
> When the Execute() function of a TThread object is done, then the thread 
> doesn't run anymore and can't run again without making a new instance of 
> your thread object. Resume() and Suspend() make no sense on a thread which 
> has left the Execute() function. If you don't wan't to make new instances 
> of your threads every time, then you should apply a state machine in your 
> thread where you can pause, run and exit your thread (everything has to be 
> done in a loop in Execute()!). Resume() and Suspend() aren't suited for 
> controlling running threads as you don't know the state of your thread 
> when it suspends/resumes.

My Execute function is running a loop already that repeats 
EstablishBehaviour followed by Suspend. My main class then uses a timer to 
Resume them so the EstablishBehaviour is run again. The Execute function is 
not ended until the simulation is terminated or the application exited. Here 
is the actual code:

void __fastcall TAgent::Execute (void)
{
 do
 {
  SetName ();
  EstablishBehaviour ();
  //debugMe (Format("SUSPENDING %d", ARRAYOFCONST((myId))).c_str());
  Suspend ();
 } while (!Terminated);
}


-- 
Matt Ross

-=I haven't lost my mind; it's backed up on tape.=- 
Technical Details
Message-ID: <429ec62c$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!news.glorb.com!news-out.newsfeeds.com!propagator3-LAX.newsfeeds.com!news-in.spamkiller.net!news-in.superfeed.net!caladan.landsraad.net!newsgroups.borland.com!not-for-mail
References: <429d7aef$1@newsgroups.borland.com> <429dd4f1@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
Reply-To: "Matt Ross" <m.dot.g.dot.ross@herts.dot.ac.dot.uk>
From: "Matt Ross" <m.dot.g.dot.ross@herts.dot.ac.dot.uk>
Newsgroups: borland.public.cppbuilder.nativeapi
References: <429d7aef$1@newsgroups.borland.com> <429dd4f1@newsgroups.borland.com>
Subject: Re: Converting existing non-threaded app to threaded
Date: Thu, 2 Jun 2005 09:40:58 +0100
Lines: 65
Organization: University of Hertfordshire
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2900.2527
X-RFC2646: Format=Flowed; Response
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2527
NNTP-Posting-Host: 147.197.236.174
Message-ID: <429ec62c$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1117701676 147.197.236.174 (2 Jun 2005 01:41:16 -0700)
Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!news.glorb.com!news-out.newsfeeds.com!propagator3-LAX.newsfeeds.com!news-in.spamkiller.net!news-in.superfeed.net!caladan.landsraad.net!newsgroups.borland.com!not-for-mail