# develop-06 9104 Spring 1991 — source transcripts, page 2

[Canonical transcript page](https://www.vintageapple.net/transcripts/media/va-develop-06-9104-spring-1991-8d7fb4/2)

## Transcribed source material

Machine transcriptions and image descriptions; uncertain copy may be marked [illegible]. Verify against the linked originals. Historical rights remain with the original owners.

These are transcribed excerpts, not the full publications. Open the originals for remaining pages.

### PDF page 12

[Cite this scan](https://www.vintageapple.net/transcripts/media/va-develop-06-9104-spring-1991-8d7fb4/2#scan-78e9ae2fec80133ece10da93652090149990678a98b240cdbb4a01aeb06bd9d9)

Transcribed text:

In this example, execution from the original thread enters the InNewThread
procedure. Two threads leave the procedure, but at different times. The original
thread goes in and is cloned before coming out. A new thread now exists, but it
hasn’t started execution yet. InNewThread tests whether the current thread’s ID is
that of the new thread, beepThread, and returns a Boolean indicating the result of
this test. It’s essentially supplying an answer to the question “Am I running the new
thread?” Since the original thread is still the current thread, it returns from
InNewThread with a value of false, thus skipping over the code contained in the IF
block. It continues execution by entering the main event loop, drawing balls, and
calling the Yield function. Each time it calls Yield, it politely gives control to other
threads that may want time to execute.
On the first call to Yield, the newly cloned thread returns from the call to
InNewThread with a value of true, indicating that this is the new thread and not the
original. The new thread enters the block of code associated with the IF statement
and begins executing the loop, which beeps and yields 30 times. Each call to Yield
exchanges control with the main event loop. The new thread lives out its life within
the confines of the IF block. After completing its task, it calls EndThread and dies.
The conditions for terminating these two threads are different: the beeping thread
ends after 30 iterations; the original thread ends when the user presses the mouse
button. The call to ExitThreads at the end of the program ensures that all threads
have completed before the program terminates.
SEMAPHORES
With multiple threads running around in your program, it’s possible for them to get
in each other’s way. The Threads Package provides a semaphore mechanism to help
you manage this problem. The problem occurs when two threads compete for a
resource. Two threads that are executing at the same time may each want exclusive
use of the same device, file, or memory location.
To deal with this situation, you assign a semaphore to control access to this resource.
Then, when you write the thread that uses the resource, you always make sure that
the thread “grabs” the semaphore. After you’re done with the resource, you
“release” the semaphore.
What happens if a thread tries to grab a semaphore that has already been grabbed?
The thread goes to sleep, waiting in a queue associated with the semaphore. When
the semaphore does become available, the sleeping thread wakes up with control of
the semaphore, completely unaware that it had to wait in the queue. It continues
executing code as usual, and releases the semaphore when it’s done, thus giving
other threads an opportunity to use the resource.
d e v e l o p   Spring 1991
10

Printed dates: Spring 1991

Languages: English

[Original PDF page 12](https://www.vintageapple.net/files/55be35a2a9d841c7e1b82c2183e0dcecd3715cf688a1caceff9e91afe507c3cc/develop-06_9104_Spring_1991.pdf#page=12)

[Previous transcribed scans](https://www.vintageapple.net/transcripts/media/va-develop-06-9104-spring-1991-8d7fb4/1.md)
