我的库中有用于 win 线程和 pthread 的可移植 TLS(线程本地存储)代码,但 TlsXXX api 在 WinRT 上不可用。然而,there is FlsXXX api that serves almost the same purpose作为the TLS api . 来自 MSDN:
A fiber can use fiber local storage (FLS) to create a unique copy of a variable for each fiber. If no fiber switching occurs, FLS acts exactly the same as thread local storage
那么,这是否意味着我可以简单地使用 FlsXXX api 作为替代品(我不使用纤程,也不对变量使用 __thread 说明符,我直接使用 api)。
最佳答案
First you have to convert the thread to a fiber.
这是您在应用商店应用中无法做到的,鸡生蛋还是蛋生鸡的问题。这是 SDK 文档所说的,但实际上并不是 Microsoft CRT 所做的,它使用 FlsAlloc() 但从不在任何地方调用 ConvertThreadToFiber/Ex()。所以你没关系,永远不要调用 CreateFiber()。
是的,如果您不创建纤程,FLS 与 TLS 相同 according to the SDK :
A fiber can use fiber local storage (FLS) to create a unique copy of a variable for each fiber. If no fiber switching occurs, FLS acts exactly the same as thread local storage. The FLS functions (FlsAlloc, FlsFree, FlsGetValue, and FlsSetValue) manipulate the FLS associated with the current thread. If the thread is executing a fiber and the fiber is switched, the FLS is also switched.
在CRT中也可以看到,看一下VS2012+的vc/crt/src/Platform.cpp源码文件,__TlsAlloc()函数。当 _CRT_APP 为#defined 时,您会看到它回退到 FlsAlloc。它用于 VCLibs 构建,即在商店应用程序中使用的构建。
没有明确记录,但非常有力的证据表明这很好。
关于windows - FLS 与 TLS,我可以使用光纤本地存储代替 TLS 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19613016/