- Sammy Johns
#===================================================================================================
# Example Script: Aztec Simple Child Thread
# Demonstrates creation of a child thread and simple thread to thread synchronization.
#===================================================================================================
# Main class that is derived from Thread. VM automatically creates us and executes Run().
class Main from<Thread>
{
method Main()
{
# Create the sync flag object so child can tell us when it's done.
ChildDoneFlag = new<SyncFlag>
}
method virtual Run()
{
StdIO.Write("In main thread (Thread " + ThreadId().Str() + "), about to create and execute child thread.")
# Create the child thread, passing it the synchronization flag, and then run it.
Child = new<ChildThread(ChildDoneFlag)>
Child.Run()
# Wait for the child to finish and release this flag.
ChildDoneFlag.WaitForAccess()
StdIO.Write("In main thread, child thread is complete and released control back to us.")
}
data<ChildThread> Child
data<SyncFlag> ChildDoneFlag
}
#-------------------------------------------------------------------------------------
# Child thread that will get created and executed by the Main thread. Once unleashed,
# it runs to completion (simple loop) and then sets flag to tell Main we're done.
#-------------------------------------------------------------------------------------
class ChildThread from<Thread>
{
method ChildThread(SyncFlag DoneFlag)
{
ChildDoneFlag = DoneFlag
}
method virtual Run()
{
data<int> Count = 1
data<int> const Limit = 5000000
StdIO.Write("Child thread (Thread " + ThreadId().Str() + ") is beginning its task.")
# Simply sit in a loop, pretending to be busy, and signal Main when complete.
while ( Count <= Limit )
{
Count.Inc()
}
StdIO.Write("Child thread completed its task (" + Limit.Str() + " loops). Releasing synchronization flag.")
# Main thread can't continue until we release it by raising this flag.
ChildDoneFlag.RaiseFlag()
}
data<SyncFlag> ChildDoneFlag
}