windows - FLS 与 TLS,我可以使用光纤本地存储代替 TLS 吗?

标签 windows multithreading windows-runtime

我的库中有用于 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/

相关文章:

javascript - Windows 8 (JavaScript) : Multiple <video> tags with same webcam source: Possible?

c# - backgroundTaskHost.exe 退出,代码为 1 (0x1)

css - 是否有 CSS 方法/hack 来指定基于平台的背景颜色(即 mac vs pc)

c - 是否建议使用 strcmp 或 _tcscmp 比较 Unicode 版本中的字符串?

java - 阻止应用程序的所有其他输入并通过 Java 包装器对其进行控制

java - JAVA中无等待队列的实现

javascript - 重新进入安全的异步函数执行

windows - 在 Cruisecontrol 中设置环境变量

multithreading - Phonegap "[' Media'] 插件应该使用后台线程。”

multithreading - 为什么我的 goroutines 在完成时互相等待而不是完成?