- Mastering C++ Multithreading
- Maya Posch
- 75字
- 2021-07-15 17:34:06
Thread local storage
Qt offers TLS through its QThreadStorage class. Memory management of pointer type values is handled by it. Generally, one would set some kind of data structure as a TLS value to store more than one value per thread, as described, for example, in the QThreadStorage class documentation:
QThreadStorage<QCache<QString, SomeClass> > caches;
void cacheObject(const QString &key, SomeClass* object) {
caches.localData().insert(key, object);
}
void removeFromCache(const QString &key) {
if (!caches.hasLocalData()) { return; }
caches.localData().remove(key);
}