next up previous contents index
Next: 1.11.2 General threading concerns Up: 1.11 Threads Previous: 1.11 Threads   Contents   Index

1.11.1 Implicit synchronization

Onyx provides mechanisms for implicit object synchronization. To see why implicit object synchronization is necessary, consider what happens when two threads concurrently modify globaldict (a perfectly legitimate thing to do, by the way). The internals of a dictionary are rather complex, and if two modifications were interleaved, havoc would ensue. Therefore, globaldict is implicitly locked. That is a good thing, except that it slows down every access to globaldict. In contrast, userdict is a per-thread dictionary, so it is not implicitly locked.

Implicit locking for new objects is controlled via setlocking , and can be queried via currentlocking . Implicit locking is turned off by default when Onyx is started up, so if an application needs to create an object that is shared among threads, it should temporarily turn on implicit locking. For example, the following code creates a stack in globaldict that can be used as a simple message queue:

currentlocking # Save for later restoration.
true setlocking

# Push globaldict onto dstack before calling def.
globaldict begin
$queue stack def
end # globaldict

# Restore implicit locking mode.
setlocking

For additional details on the mechanics of implicit synchronization, see Section 2.7.1.


next up previous contents index
Next: 1.11.2 General threading concerns Up: 1.11 Threads Previous: 1.11 Threads   Contents   Index
Jason Evans 2005-03-16