← develop-06 9104 Spring 1991

develop-06 9104 Spring 1991

From the originals

13 scans

Machine-transcribed text and image descriptions. Check the linked original for uncertain words or fine print.

These are transcribed pages from the documents, not the full publications. Open the original for the remaining pages.

PDF page 12

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