|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 )
semaphore.lock( "ThreadSafeGetUnique", 3600 )
newItemAdr = table.uniqueName( prefix, insideTableAdr, places )
new( newObjectType, newItemAdr )
semaphore.unlock( "ThreadSafeGetUnique" )
return ( newItemAdr )
|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.|