QThreadStorage 類

The QThreadStorage class provides per-thread data storage. 更多...

頭: #include <QThreadStorage>
qmake: QT += core

注意: 此類的所有函數 綫程安全 .

公共函數

QThreadStorage ()
~QThreadStorage ()
bool hasLocalData () const
T & localData ()
T localData () const
void setLocalData (T data )

詳細描述

The QThreadStorage class provides per-thread data storage.

QThreadStorage is a template class that provides per-thread data storage.

The setLocalData () 函數為調用綫程存儲單特定綫程值。以後數據可以被訪問使用 localData ().

The hasLocalData () 函數允許程序員確定先前是否有設置數據使用 setLocalData () 函數。這對惰性初始化也很有用。

若 T 是指針類型, QThreadStorage takes ownership of the data (which must be created on the heap with new ) 並在綫程正常退齣或憑藉終止退齣時刪除它。

For example, the following code uses QThreadStorage to store a single cache for each thread that calls the cacheObject() and removeFromCache() functions. The cache is automatically deleted when the calling thread exits.

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);
}
					
					

告誡

另請參閱 QThread .

成員函數文檔編製

QThreadStorage:: QThreadStorage ()

構造新的每綫程數據存儲對象。

QThreadStorage:: ~QThreadStorage ()

銷毀每綫程數據存儲對象。

注意:存儲的每綫程數據不會被刪除。任何數據留在 QThreadStorage 會被泄漏。確保所有綫程使用 QThreadStorage 已退齣再刪除 QThreadStorage .

另請參閱 hasLocalData ().

bool QThreadStorage:: hasLocalData () const

若 T 是指針類型,返迴 true 若調用綫程有非零可用數據。

若 T 是值類型,返迴數據是否已經被構造通過調用 setLocalData or localData .

另請參閱 localData ().

T &QThreadStorage:: localData ()

返迴由調用綫程設置的數據的引用。

若未設置數據,這將創建默認 T 類型構造實例。

另請參閱 setLocalData () 和 hasLocalData ().

T QThreadStorage:: localData () const

這是重載函數。

返迴由調用綫程設置的數據的副本。

另請參閱 hasLocalData ().

void QThreadStorage:: setLocalData ( T data )

把調用綫程的本地數據設為 data 。以後可以訪問它使用 localData () 函數。

若 T 是指針類型, QThreadStorage 擁有數據的所有權,並自動刪除它當綫程退齣 (正常或憑藉終止) 時或當 setLocalData() 被再次調用。

另請參閱 localData () 和 hasLocalData ().