what exactly is fiber safe optimizations in VC++? what exactly is fiber safe optimizations in VC++? multithreading multithreading

what exactly is fiber safe optimizations in VC++?


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 )