Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what exactly is fiber safe optimizations in VC++?

I was reading about Fiber Safe optimizations on MSDN. It says that

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

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?

like image 882
Destructor Avatar asked Jun 25 '15 06:06

Destructor


1 Answers

Fibers (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

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.

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.

--- 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 )

like image 145
kfsone Avatar answered Nov 03 '22 14:11

kfsone