Article View: borland.public.cppbuilder.nativeapi
Article #8992Re: Wait for "signal" from another application and/or thread
From: Bob Gonder
Date: Sun, 15 Aug 2004 01:05
Date: Sun, 15 Aug 2004 01:05
59 lines
3396 bytes
3396 bytes
Bob Gonder wrote:
>GlobalButtonclickEvent is manual reset type.
>ThreadEvent is semaphore type.
>Buttonclick posts PulseEvent() to GlobalButtonclickEvent.
>Thread0 in each app is waiting for GlobalButtonclickEvent.
>Each Thread0 posts a ReleaseSemaphore() of 3, starting 3 worker
>threads.
>That would take care of how many to run. Now, how do you make sure
>that each thread runs the same number of times? (that the faster
>thread doesn't steal a cycle from a slower one).
>Each thread could have a counter.
>Thread0 keeps a count of how many times it has released 3 threads.
>If WorkerThread1 wakes up and sees that it's counter >= Thread0
>counter, then it posts a ReleaseSemaphore() of 1 (to make up for
>snagging one by mistake), sleeps a bit, the waits again.
>How big should the counter be? how many times is this cycle going to
>occur? There are other more involved methods to ensure counter doesn't
>overflow. But if this is a manual operation, the billion or so clicks
>supported by a long count should suffice.
With that in mind, a simpler method:
Still 4 threads, One CounterThread, Three WorkerThreads.
All threads wait for ManualReset type Event.
OnClick() sends PulseEvent.
CounterThread adds 1 to GlobalCount. the goes back and waits for
another event.
WorkerThreads, if not already running, start running when Event comes.
WorkerThreads check GlobalCount against their own count, and recycle
if not equal. Otherwise, they wait.
unsigned long GlobalCount=0;
CounterThread()
{
while( !Terminated)
{ WaitForObject()
++GlobalCount;
};
}
WorkerThread()
{ unsigned long Count=0;
while( !Terminated)
{ if( GlobalCount==Count )
WaitForObject()
++Count;
Sleep(rand());
};
}
CounterThread, being so short, should catch every Pulse.
If WorkerThreads miss the Pulse, they should still see a difference in
Count and run again. There is a small race condition though. If the
Pulse comes After the WorkerThread checks Count, and Before WaitFor is
processed, then it could miss it. But, it will make up for it the next
time.
Technical Details
Message-ID:
<oocth0hsru2ohfnmsd0br2t7uqd2n3m0il@4ax.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!news.maxwell.syr.edu!newsfeed.icl.net!news-FFM2.ecrc.net!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mailReferences:
<4105617c$1@newsgroups.borland.com> <4105777f$3@newsgroups.borland.com> <411ccf89@newsgroups.borland.com> <gnsph0pq4c8mfhapah6cideqmbp6gqor5r@4ax.com> <411d2245@newsgroups.borland.com> <pboqh013avflntd8vgj1vcbrg000u94vb7@4ax.com> <411dac31$1@newsgroups.borland.com> <8issh0t012qrfjg8g3i0icdpru3h7ejs0n@4ax.com>Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,b6552239fe09c315 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: Wait for "signal" from another application and/or thread Date: Sat, 14 Aug 2004 18:05:51 -0700 Organization: MindSpring Enterprises, Inc. Reply-To: notbg@notmindspring.invalid Message-ID: <oocth0hsru2ohfnmsd0br2t7uqd2n3m0il@4ax.com> References: <4105617c$1@newsgroups.borland.com> <4105777f$3@newsgroups.borland.com> <411ccf89@newsgroups.borland.com> <gnsph0pq4c8mfhapah6cideqmbp6gqor5r@4ax.com> <411d2245@newsgroups.borland.com> <pboqh013avflntd8vgj1vcbrg000u94vb7@4ax.com> <411dac31$1@newsgroups.borland.com> <8issh0t012qrfjg8g3i0icdpru3h7ejs0n@4ax.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.255.185 X-Trace: newsgroups.borland.com 1092531948 69.69.255.185 (14 Aug 2004 18:05:48 -0700) Lines: 61 Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!news.maxwell.syr.edu!newsfeed.icl.net!news-FFM2.ecrc.net!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail