Article View: borland.public.cppbuilder.nativeapi
Article #8990Re: Wait for "signal" from another application and/or thread
From: Bob Gonder
Date: Sat, 14 Aug 2004 20:48
Date: Sat, 14 Aug 2004 20:48
38 lines
3050 bytes
3050 bytes
<zoom> wrote: >Please copy/paste the following code and observe how CreateEvent and >WaitForSingleObject works. Can't. I don't do VCL. >In another word if we run 4 instances of this program we will have 12 >threads (each app has 3 threads) and when we >press the Button1 on any of 4 instances all threads will increment their >count by one regardless how quickly we press the Button1. Look at semaphores. They have a count value. If your buttonclick does ReleaseSemaphore() with a count of 12, then 12 threads will run. Maybe thread 1 will run twice because it was faster than thread 7 and grabbed the semaphore before 7 got done with the previous go-around. How do you figure out how many instances are running so you know to trigger 12 threads? Well, you could do it with 2 events instead of one. 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.
Technical Details
Message-ID:
<8issh0t012qrfjg8g3i0icdpru3h7ejs0n@4ax.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!news.maxwell.syr.edu!newsfeed.frii.net!newsfeed.frii.net!news.compaq.com!newsfeeds.ihug.co.nz!ihug.co.nz!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>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 13:48:13 -0700 Organization: MindSpring Enterprises, Inc. Reply-To: notbg@notmindspring.invalid Message-ID: <8issh0t012qrfjg8g3i0icdpru3h7ejs0n@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> 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 1092516491 69.69.255.185 (14 Aug 2004 13:48:11 -0700) Lines: 40 Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!news.maxwell.syr.edu!newsfeed.frii.net!newsfeed.frii.net!news.compaq.com!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail