草庐IT

TlsSetValue

全部标签

c++ - Windows 的线程本地存储是否初始化值?

我在MSDN中发现了关于线程本地存储初始值的矛盾。Thispage说:Whenthethreadsarecreated,thesystemallocatesanarrayofLPVOIDvaluesforTLS,whichareinitializedtoNULL.这让我相信,如果我从一个从未为同一索引调用过TlsSetValue的线程中使用有效索引调用TlsGetValue,那么我应该得到一个空指针。Thispage,然而,说:Itisuptotheprogrammertoensure...thatthethreadcallsTlsSetValuebeforecallingTlsGet

在tlssetValue之后,先前的对象变为null

在我的C++DLL中,我使用线程本地存储存储并检索线程特定的信息。线程特定的信息包装在对象中pInfo。我是在DLL附加中分配一次,然后使用TLSSetValue使用将PINFO存储为每个线程。PInfo*pInfo=TlsGetValue(tlsIndex);if(pInfo==NULL){pInfo=newPInfo();errCheck=TlsSetValue(tlsIndex,pInfo);}经过一些功能回调并填充Pinfo。在特定点,我将此Pinfo(指针)推向静态向量(定义为下文)。vector*swapBucket;使用swapBucket->push_back(pInfo);我