Mixed Up Threads
Volume Number: 13
Issue Number: 7
Column Tag: develop
by Andy Bachorski and George Warner
See if you can solve this puzzle in the form of a dialog between a pseudo KON (Andy
Bachorski) and BAL (George Warner). The dialog gives clues to help you. Keep
guessing until you're done; your score is the number to the left of the clue that gave
you the correct answer. Even if you never run into the particular problems being
solved here, you'll learn some valuable debugging techniques that will help you solve
your own programming conundrums. And you'll also learn interesting Macintosh
trivia.
KON So, BAL, I've run into a weird problem. I crash when I try to compile and execute
a script using the AppleScript Open Scripting Architecture component from within a
native PowerPC(tm) application.
BAL What's so weird about that? AppleScript hasn't been touched in years. It's bound
to be showing its age by now.
KON Yeah, sure. Anyway, I've got an application that creates several threads.
BAL Now you're throwing the Thread Manager into the mix?
KON Look, can I just explain the problem? As I said, several threads are created, and
inside each thread a string containing a valid AppleScript script is passed to the
AppleScript component to be compiled and executed.
BAL This one's easy. You're using a single AppleScript component instance for all the
threads. You need to have a separate instance for each thread for this scheme to work.
100
KON Wrong. Why don't you let me finish? Inside each thread we ask for and get an
AppleScript component instance. Next we call OSACompileExecute to tell AppleScript to
compile the script text. The thread terminates when OSACompileExecute returns.
BAL Hmm. If all you're doing in each thread is calling OSACompileExecute, how are the
other threads getting time before the first one terminates?
90
KON Well, the AppleScript component lets you install an active function that gets
called periodically during the compilation and execution of a script. Normally you'd
call WaitNextEvent to give time to other processes, but here we call YieldToAnyThread