Article View: borland.public.cppbuilder.nativeapi
Article #8996Re: Wait for "signal" from another application and/or thread
From: zoom
Date: Sun, 15 Aug 2004 11:24
Date: Sun, 15 Aug 2004 11:24
67 lines
3628 bytes
3628 bytes
Thank you for your very valuable postings.
Is there any limit with the number of events, semaphores created in
WindowsXP ?
"Bob Gonder" <notbg@notmindspring.invalid> wrote in message
news:oocth0hsru2ohfnmsd0br2t7uqd2n3m0il@4ax.com...
> 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:
<411f480a@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!proxad.net!fr.ip.ndsoftware.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> <oocth0hsru2ohfnmsd0br2t7uqd2n3m0il@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: <zoom> Newsgroups: borland.public.cppbuilder.nativeapi 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> <oocth0hsru2ohfnmsd0br2t7uqd2n3m0il@4ax.com> Subject: Re: Wait for "signal" from another application and/or thread Date: Sun, 15 Aug 2004 21:24:53 +1000 Lines: 69 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2800.1437 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1441 NNTP-Posting-Host: 211.30.68.31 Message-ID: <411f480a@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1092569098 211.30.68.31 (15 Aug 2004 04:24:58 -0700) Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!proxad.net!fr.ip.ndsoftware.net!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail