I was reading about Fiber Safe optimizations on MSDN. It says that
我在MSDN上读到纤维安全优化。它说
Data declared with __declspec(thread) is referenced through a thread-local storage (TLS) array. The TLS array is an array of addresses that the system maintains for each thread. Each address in this array gives the location of thread-local storage data. A fiber is a lightweight object that consists of a stack and a register context and can be scheduled on various threads. A fiber can run on any thread. Because a fiber may get swapped out and restarted later on a different thread, the address of the TLS array must not be cached or optimized as a common sub expression across a function call
使用__declspec(thread)声明的数据可以通过一个线程本地存储(TLS)数组进行引用。TLS数组是系统为每个线程维护的地址数组。这个数组中的每个地址都给出线程本地存储数据的位置。纤程是一个轻量级对象,由堆栈和寄存器上下文组成,可以安排在不同的线程上。纤维可以在任何线上运行。由于光纤可能会被换出并在不同的线程上重新启动,所以TLS数组的地址不能被缓存或优化为通过函数调用的公共子表达式。
What are fiber safe optimizations? What is the actual purpose of using it? Why they are saying that "Because a fiber may get swapped out and restarted later on a different thread, the address of the TLS array must not be cached or optimized as a common sub expression across a function call."? Why and when should it be prevented?
什么是纤维安全优化?使用它的实际目的是什么?为什么他们会说“因为一个光纤可能在稍后的不同线程上被交换并重新启动,所以TLS数组的地址不能作为一个函数调用的公共子表达式进行缓存或优化”?为什么和什么时候应该预防它?
1 个解决方案
#1
16
Fiber
s (in this context) are an MS specific technology that lets you manually control scheduling of "light-weight" threads-of-work, but they co-exist with threads. https://msdn.microsoft.com/en-us/library/windows/desktop/ms682661(v=vs.85).aspx
纤维(在此上下文中)是一种MS特定技术,可以让您手动控制“轻量级”线程的调度,但它们与线程共存。https://msdn.microsoft.com/en-us/library/windows/desktop/ms682661(v = vs.85). aspx
Imagine you have a fiber that has a long piece of work to do and two worker threads.
假设您有一个纤维,它有一长段工作要做,有两个工作线程。
The fiber runs on one thread and is descheduled. Then the next thread gets processor time. It finds that the fiber needs to run, so it runs the fiber.
纤维在一根线上运行,并被分离。然后,下一个线程获得处理器时间。它发现纤维需要运行,所以它运行纤维。
So far, not a problem. Unless you are using thread local storage.
到目前为止,这还不是问题。除非使用线程本地存储。
__declspec(thread) int global_int;
Every thread you create sees its own unique instance of this variable. If your fiber code is using variables like this and you allow the fiber to transition between threads, then the underlying variable might change. The most obvious of these is, of course, the thread id
.
您创建的每个线程都看到这个变量的唯一实例。如果您的光纤代码使用的是这样的变量,并且允许光纤在线程之间转换,那么底层的变量可能会改变。其中最明显的当然是线程id。
void fiber_in_your_diet() {
Queue& thread_queue = g_threadQueues[std::thread::get_id()];
// long work that gets transferred to a different thread
thread_queue.push_back(something); // wrong queue!
}
"Fiber Safe Optimizations" is a misnomer. You only need "/GT" if you are using Fibers and the chances are you aren't. You'd know if you were, partly by the soul-consuming hatred for life you woke up with in the morning, and partly by the way you would know what Fibers were.
“光纤安全优化”是用词不当。你只需要“/GT”,如果你使用的是纤维,而你不需要。你会知道,如果你是,一部分是因为对生活的强烈的厌恶,你在早上醒来,部分是因为你知道什么是纤维。
--- EDIT ---
推荐- - - - - -编辑- - - - - -
"Fiber" is fairly widely used to describe a "light-weight" unit of execution that doesn't have the bells and whistles of an operating system thread, in particular it doesn't run automatically. Depending on your requirements it's actually possible for Fibers to be less expensive than threads. They are very often associated with coroutines (see https://en.wikipedia.org/wiki/Fiber_(computer_science)#Fibers_and_coroutines ). Note that future versions of the C++ language may include a standard implementation of the Fiber concept (see http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4024.pdf )
“Fiber”被广泛用于描述一种“轻量级”执行单元,它没有操作系统线程的功能,特别是它不会自动运行。根据您的需求,纤维实际上可能比线程更便宜。它们通常与coroutines相关(参见https://en.wikipedia.org/wiki/Fiber_(computer_science)#Fibers_and_coroutines)。注意,未来的c++语言版本可能包含纤维概念的标准实现(参见http://www.open-std.org/jtc1/sc22/ wg2.1 / docs/papers/papers/2014/n4024.pdf)
#1
16
Fiber
s (in this context) are an MS specific technology that lets you manually control scheduling of "light-weight" threads-of-work, but they co-exist with threads. https://msdn.microsoft.com/en-us/library/windows/desktop/ms682661(v=vs.85).aspx
纤维(在此上下文中)是一种MS特定技术,可以让您手动控制“轻量级”线程的调度,但它们与线程共存。https://msdn.microsoft.com/en-us/library/windows/desktop/ms682661(v = vs.85). aspx
Imagine you have a fiber that has a long piece of work to do and two worker threads.
假设您有一个纤维,它有一长段工作要做,有两个工作线程。
The fiber runs on one thread and is descheduled. Then the next thread gets processor time. It finds that the fiber needs to run, so it runs the fiber.
纤维在一根线上运行,并被分离。然后,下一个线程获得处理器时间。它发现纤维需要运行,所以它运行纤维。
So far, not a problem. Unless you are using thread local storage.
到目前为止,这还不是问题。除非使用线程本地存储。
__declspec(thread) int global_int;
Every thread you create sees its own unique instance of this variable. If your fiber code is using variables like this and you allow the fiber to transition between threads, then the underlying variable might change. The most obvious of these is, of course, the thread id
.
您创建的每个线程都看到这个变量的唯一实例。如果您的光纤代码使用的是这样的变量,并且允许光纤在线程之间转换,那么底层的变量可能会改变。其中最明显的当然是线程id。
void fiber_in_your_diet() {
Queue& thread_queue = g_threadQueues[std::thread::get_id()];
// long work that gets transferred to a different thread
thread_queue.push_back(something); // wrong queue!
}
"Fiber Safe Optimizations" is a misnomer. You only need "/GT" if you are using Fibers and the chances are you aren't. You'd know if you were, partly by the soul-consuming hatred for life you woke up with in the morning, and partly by the way you would know what Fibers were.
“光纤安全优化”是用词不当。你只需要“/GT”,如果你使用的是纤维,而你不需要。你会知道,如果你是,一部分是因为对生活的强烈的厌恶,你在早上醒来,部分是因为你知道什么是纤维。
--- EDIT ---
推荐- - - - - -编辑- - - - - -
"Fiber" is fairly widely used to describe a "light-weight" unit of execution that doesn't have the bells and whistles of an operating system thread, in particular it doesn't run automatically. Depending on your requirements it's actually possible for Fibers to be less expensive than threads. They are very often associated with coroutines (see https://en.wikipedia.org/wiki/Fiber_(computer_science)#Fibers_and_coroutines ). Note that future versions of the C++ language may include a standard implementation of the Fiber concept (see http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4024.pdf )
“Fiber”被广泛用于描述一种“轻量级”执行单元,它没有操作系统线程的功能,特别是它不会自动运行。根据您的需求,纤维实际上可能比线程更便宜。它们通常与coroutines相关(参见https://en.wikipedia.org/wiki/Fiber_(computer_science)#Fibers_and_coroutines)。注意,未来的c++语言版本可能包含纤维概念的标准实现(参见http://www.open-std.org/jtc1/sc22/ wg2.1 / docs/papers/papers/2014/n4024.pdf)