?
本文檔使用 PHP中文網(wǎng)手冊(cè) 發(fā)布
在頭文件<threads.h>中定義 | ||
---|---|---|
int tss_create(tss_t * tss_key,tss_dtor_t析構(gòu)函數(shù)); | (自C11以來) |
創(chuàng)建新的線程專有存儲(chǔ)密鑰并將其存儲(chǔ)在tss_key指向的對(duì)象中。 盡管不同線程可能使用相同的鍵值,但由tss_set綁定到鍵的值將以每個(gè)線程為基礎(chǔ)進(jìn)行維護(hù),并在調(diào)用線程的整個(gè)生命周期內(nèi)保持不變。
值NULL與所有現(xiàn)有線程中新創(chuàng)建的鍵相關(guān)聯(lián),創(chuàng)建線程后,與所有TSS鍵相關(guān)的值將初始化為NULL。
如果析構(gòu)函數(shù)不是空指針,那么還會(huì)關(guān)聯(lián)由thrd_exit(但不是由tss_delete而不是在程序終止處通過退出)釋放存儲(chǔ)器時(shí)調(diào)用的析構(gòu)函數(shù)。
從特定于線程的存儲(chǔ)析構(gòu)函數(shù)調(diào)用tss_create會(huì)導(dǎo)致未定義的行為。
tss_key | - | 指向內(nèi)存位置的指針以存儲(chǔ)新的線程專用存儲(chǔ)鍵 |
---|---|---|
destructor | - | 指向在線程退出時(shí)調(diào)用的函數(shù)的指針 |
此函數(shù)的POSIX等價(jià)物是pthread_key_create。
如果成功則返回 thrd_success,否則返回 thrd_error。
int thread_func(void *arg) { tss_t key; if (thrd_success == tss_create(&key, free)) { tss_set(key, malloc(4)); // stores a pointer on TSS // ... }} // calls free() for the pointer stored on TSS
C11標(biāo)準(zhǔn)(ISO / IEC 9899:2011):
7.26.6.1 tss_create函數(shù)(p:386)