![]() |
||
| Home | SwiftForth Archive | SwiftX Archive | |

Hi,
Having need to synchronize two threads, and to communicate local information
>from the created thread back to the creator, I have investigated the event
signaling api in Windows. Here is the code required to do such a synchronization.
This code may be found in the faq at
http://www.neverslow.com/cgi-bin/swiftforth?file=127
Thanks,
Rick
{ ====================================================================
(C) Copyright 2000 FORTH, Inc. Rick VanNorman rvn_at_forth.com
An example of using events to synchronize two threads so that the
creator can retrieve information about the created.
==================================================================== }
REQUIRES FORK \ Previously published here, available at the FAQ
1 IMPORT: SetEvent
4 IMPORT: CreateEvent
2 IMPORT: WaitForSingleObject
{ --------------------------------------------------------------------
TEST forks, then gets the event handle to signal and replaces it with
the data that the creator was interested in. Finally the event is
signaled and any other processing is done. Try it with and without
the Sleep function to verify that we can detect the signal.
-------------------------------------------------------------------- }
\ addr contains the event to signal
\ when signaled, the address will contain the object of interest
: TEST ( addr -- )
FORKS()> \ 2000 Sleep DROP
DUP @ 99 ROT ! SetEvent DROP ;
{ --------------------------------------------------------------------
TRY creates an event, keeping its handle at HERE and forks TEST. It
then waits for either the event to be signaled by the SetEvent in TEST
or for a 1000 ms timeout. We know if the event is signaled that the
new thread has returned data at HERE for us to use.
-------------------------------------------------------------------- }
: TRY ( -- )
0 1 0 0 CreateEvent DUP >R HERE !
HERE TEST DROP
R@ 1000 WaitForSingleObject R> CloseHandle DROP
WAIT_OBJECT_0 <> THROW HERE @ ;
Received on Fri Oct 27 2000 - 21:35:43 PDT
This archive was generated by hypermail 2.2.0 : Thu Nov 20 2008 - 03:04:29 PST