Frontier Tutorials / Working With Threads / Thread Utilities

This fatpage contains the ThreadSafeGetUnique utility script.


ThreadSafeGetUnique( newObjectType, prefix, insideTableAdr, places=nil )

If a threaded script needs to create a new scratchpad table in a standard location, table.uniqueName is the obvious tool to use. But table.uniqueName simply returns an unused address. And between the time you call table.uniqueName and the time you call new to create the new object, another instance of your script (in another thread) may call table.uniqueName and end up with the same address! So you have to protect the sequence of calls to table.uniqueName and new with a semaphore.

ThreadSafeGetUnique locks a semaphore, calls table.uniqueName to get a unique address, creates an object of the type you specify, unlocks the semaphore, and returns the address of the new object. Just what you'd have to do, but this makes it a single call. So your program becomes easier to understand and maintain.

on ThreadSafeGetUnique( newObjectType, prefix, insideTableAdr, places=nil )
space picturesemaphore.lock( "ThreadSafeGetUnique", 3600 )
space picturelocal
space picturenewItemAdr = table.uniqueName( prefix, insideTableAdr, places )
space picturenew( newObjectType, newItemAdr )
space picturesemaphore.unlock( "ThreadSafeGetUnique" )
space picturereturn ( newItemAdr )

On to the Glossary of Terms.

space picture
space picture
fatPage picture space picture This page is a Fat Page. It includes script(s) encoded by and for Frontier. To retrieve the script(s), save the page as source text and open it using the File->Open command.
space picture
space picture

Tutorial Contents
Working With Threads
What Are Threads?
Semaphores--Traffic Control for Threads
How to Be Thread-Friendly
Rules of Thread Safety
An Example
Frontier's Thread Verbs
Thread Utilities
Glossary of Terms
About the Author