1# fiber.h 2 3<!--Kit: Function Flow Runtime Kit--> 4<!--Subsystem: Resourceschedule--> 5<!--Owner: @chuchihtung; @yanleo--> 6<!--Designer: @geoffrey_guo; @huangyouzhong--> 7<!--Tester: @lotsof; @sunxuhao--> 8<!--Adviser: @foryourself--> 9 10## Overview 11 12A fiber is a lightweight user mode thread that enables efficient task scheduling and context switching within the user space. The **fiber.h** file declares the related APIs in C. 13 14**Library**: libffrt.z.so 15 16**System capability**: SystemCapability.Resourceschedule.Ffrt.Core 17 18**Since**: 20 19 20**Related module**: [FFRT](capi-ffrt.md) 21 22## Summary 23 24### Function 25 26| Name| Description| 27| -- | -- | 28| [FFRT_C_API int ffrt_fiber_init(ffrt_fiber_t* fiber, void(\*func)(void*), void* arg, void* stack, size_t stack_size)](#ffrt_fiber_init) | Initializes a fiber. The initialized fiber instance can store contexts.| 29| [FFRT_C_API void ffrt_fiber_switch(ffrt_fiber_t* from, ffrt_fiber_t* to)](#ffrt_fiber_switch) | Switches between fibers. The thread that calls this function suspends the current task, saves the context to the **from** fiber, and restores the context of the **to** fiber.| 30 31## Function Description 32 33### ffrt_fiber_init() 34 35``` 36FFRT_C_API int ffrt_fiber_init(ffrt_fiber_t* fiber, void(*func)(void*), void* arg, void* stack, size_t stack_size) 37``` 38 39**Description** 40 41Initializes a fiber. The initialized fiber instance can store contexts. 42 43**Since**: 20 44 45 46**Parameters** 47 48| Name| Description| 49| -- | -- | 50| fiber | Pointer to the fiber to be initialized. For details, see [ffrt_fiber_t](capi-ffrt-ffrt-fiber-t.md).| 51| func | Method to be executed after fiber switching.| 52| void* arg | Pointer to the argument of the method.| 53| void* stack | Pointer to the fiber stack memory.| 54| size_t stack_size | Fiber stack size. For details, see [ffrt_storage_size_t](capi-type-def-h.md#ffrt_storage_size_t).| 55 56**Returns** 57 58| Type| Description| 59| -- | -- | 60| FFRT_C_API int (ffrt_fiber_t* fiber, void(*func) | If the initialization is successful, **ffrt_success** is returned. Otherwise, **ffrt_error** is returned.| 61 62### ffrt_fiber_switch() 63 64``` 65FFRT_C_API void ffrt_fiber_switch(ffrt_fiber_t* from, ffrt_fiber_t* to) 66``` 67 68**Description** 69 70Switches between fibers. The thread that calls this function suspends the current task, saves the context to the **from** fiber, and restores the context of the **to** fiber. 71 72**Since**: 20 73 74 75**Parameters** 76 77| Name| Description| 78| -- | -- | 79| [ffrt_fiber_t](capi-ffrt-ffrt-fiber-t.md)* from | Pointer to the fiber to be saved.| 80| [ffrt_fiber_t](capi-ffrt-ffrt-fiber-t.md)* to | Pointer to the fiber to be restored.| 81