12th Chapter Fiber (Fiber)

Source: Internet
Author: User

12.1 Introduction to Fiber-Path objects

(1) Fiber and thread comparison

Comparison

Threads (thread)

Fiber (Fiber)

Implementation method

It's a kernel object.

A lightweight thread implemented in user mode is a smaller scheduling unit than a thread.

Dispatch mode

Dispatched by the Microsoft-defined algorithm, the operating system knows the thread at its fingertips. Kernel-to-thread scheduling is preemptive.

We call SwitchToFiber to dispatch, and the kernel knows nothing about fiber. A thread can execute only one fiber code at a time, and scheduling between fibers is not preemptive.

Note

① a thread can contain one or more fibers . The operating system can potentially take away the operation of a fiber thread at any time. When the thread is dispatched, the currently selected fiber runs, and the other fibers cannot run, because only one fiber is running at a time in the same thread , unless the call to SwitchToFiber can switch to another fiber to execute. Unlike SwitchToThread, SwitchToFiber immediately switches to another fiber to execute (if the thread has the remaining CPU time), and SwitchToThread waits for the CPU to dispatch another thread.

fibers, like threads, have their own register environment and function call stacks .

(2) Composition of the execution context of the fiber (similar to the thread context)--approximately 200 bytes

① user-defined value that is initialized to the value of the Pvparam parameter passed to ConvertThreadToFiber

② the head of a structured exception handling chain

③ stacks the top and bottom memory addresses (this is also the line stacks when we convert a thread to a fiber)

④ some CPU registers, including stack pointers, instruction pointers, and other registers (note that the floating-point status information of the CPU is not included by default)

(3) Fiber operation dynamic

★ Note:

Switching between the two fibers created on the same line thread is safe (in the A arrow), but switching between two fibers across threads is unsafe (b, c arrows in). Because the fiber is essentially thread-dispatched , it is assumed that at some point, thread 2 is calling fiber 2.2, but within fiber 2.2 The internal call of SwitchToFiber switches to fiber 1.2. If the next time period of the CPU is still given to thread 2, because the kernel does not know the fiber switch , so at this time the CPU will still try to execute the fiber 2.2 code, but because of the fiber switch, will cause the stack environment of thread 2 has changed, In this case, the implementation of fiber 2.2 may cause errors.

12.2 Use of fiber

(1) To create the main fiber : Createthreadtofiber (pvparam) (the thread to fiber, which can call other fiber API functions, can be understood as the start thread of fiber mode )

★ Note:

The ① return value is a fiber context that can be understood as returning a fiber object.

② by default, the FPU information of the x86 CPU is not saved by the fiber, so the data can be corrupted when floating-point operations are performed. To avoid this, tune the new Convertthreadtofiberex function and pass in the Fiber_flag_float_switch flag for dwflags.

(2) Create fiber (can be understood as sub-fiber ): CreateFiber

Parameters

Describe

DWORD dwstacksize

Fiber stack size. Typically incoming 0, which means the system is automatically assigned

Pfiber_start_routine

Pfnstartaddress

Fiber function, the prototype is

VOID WINAPI Fiberfunc (PVOID pvparam)

PVOID Pvparam

Additional parameters passed to the fiber function.

★ Note:

The ① return value is a fiber context that can be understood as returning a fiber object.

② Similarly, to prevent the occurrence of floating-point arithmetic accidents , you can call the new API function Createfiberex and pass in the Fiber_flag_float_switch flag.

(3) Fiber scheduling :switchtofiber (PVOID pvfiberexcutioncontext) function, The parameters are the fiber objects returned by CreateFiber or Createthreadtofiber (that is, the fiber context). Note: SwitchToFiber is the only way to get the CPU time from fiber! Since we have to show the call to SwitchToFiber to give the fiber a chance to execute, the fiber schedule is completely in our hands.

Internal operation of the ①switchtofiber function

A. Save the current values of some CPU registers (including the instruction pointer register and the stack pointer register) to the execution context of the currently running fiber.

B. From the execution context of the fibers that will be running, load the previously saved registers into the CPU registers. Use the new fiber runtime environment (such as stack, instruction pointer) when the thread continues execution

C. Associate the new fiber context with the thread, and let the thread run the specified fiber.

D. Set the instruction pointer of the thread to the instruction pointer previously saved by the new fiber, so that the thread (fiber) will continue to execute from the point where it was last executed.

(4) Removal of fiber : DeleteFiber (PVOID pvfiberexecutioncontext);

① when the fiber is executed, the function is called to destroy the fiber, the deleted fiber stack is destroyed, and the context of the fiber execution is freed.

② If the fiber is the main fiber of the ConvertThreadToFiber conversion, when the call DeleteFiber is equivalent to calling ExitThread to terminate the thread directly. If you do not want to terminate the thread, you can call Convertfibertothread to turn the main fiber back to the thread, and this will also release the last piece of memory that was used to call Converthreadtofiber to convert the thread to fiber. Note that the Convertfibertothread only converts the main fiber and is not valid for other sub-fibers .

"Fiber Program"

12th Chapter Fiber (Fiber)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.