23.9. Custom Node Basics

Fundamentally, the issue for thread safety is data shared by multiple threads. For example, a class static variable. One thread might store a value in the variable and expect it to retain that value, but a second thread might put a different value in the variable (even while the first thread is using the variable). If the variable is a pointer to allocated memory, one thread might store a different pointer (or delete the memory) while another thread is using the memory. These thread conflicts can easily cause a crash or at least incorrect results.

Our basic tools are the mutex, read/write lock, and Instance Thread Storage (or Thread Local Storage in older code). All of these are discussed in any book on multithreaded programming. We will not discuss their details here. Open Inventor provides these tools as convenient platform-independent classes. SbThreadMutex( C++ )and SbThreadRWMutex (etc)are documented in the Open Inventor online help. SoNodeTLSis described in this document because it is only of interest to programmers extending the Open Inventor toolkit. Open Inventor also provides some convenience classes like SbThreadAutoLock( C++ ) , which automatically unlocks its mutex when the SbThreadAutoLock( C++ )object goes out of scope.