1e41f4b71Sopenharmony_ci# Native VSync Development (C/C++) 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ci## When to Use 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ciThe **NativeVSync** module is used to obtain virtual synchronization (VSync) signals from the system. It provides APIs for creating and destroying an **OH_NativeVSync** instance and setting the VSync callback function, which is triggered when a VSync signal arrives. 6e41f4b71Sopenharmony_ci 7e41f4b71Sopenharmony_ci## Available APIs 8e41f4b71Sopenharmony_ci 9e41f4b71Sopenharmony_ci| API| Description| 10e41f4b71Sopenharmony_ci| -------- | -------- | 11e41f4b71Sopenharmony_ci| OH_NativeVSync_Create (const char \*name, unsigned int length) | Creates an **OH_NativeVSync** instance. A new **OH_NativeVSync** instance is created each time this API is called.| 12e41f4b71Sopenharmony_ci| OH_NativeVSync_Destroy (OH_NativeVSync \*nativeVsync) | Destroys an **OH_NativeVSync** instance.| 13e41f4b71Sopenharmony_ci| OH_NativeVSync_FrameCallback (long long timestamp, void \*data) | Sets a callback function. **timestamp** indicates the timestamp, and **data** indicates a pointer to the input parameters of the callback function.| 14e41f4b71Sopenharmony_ci| OH_NativeVSync_RequestFrame (OH_NativeVSync \*nativeVsync, OH_NativeVSync_FrameCallback callback, void \*data) | Requests the next VSync signal. When the signal arrives, a callback function is invoked.| 15e41f4b71Sopenharmony_ci 16e41f4b71Sopenharmony_ciFor details about the APIs, see [native_vsync](../reference/apis-arkgraphics2d/_native_vsync.md). 17e41f4b71Sopenharmony_ci 18e41f4b71Sopenharmony_ci## How to Develop 19e41f4b71Sopenharmony_ci 20e41f4b71Sopenharmony_ciThe following steps describe how to use the native APIs provided by **NativeVSync** to create and destroy an **OH_NativeVSync** instance and set the VSync callback function. 21e41f4b71Sopenharmony_ci 22e41f4b71Sopenharmony_ci**Adding Dynamic Link Libraries** 23e41f4b71Sopenharmony_ci 24e41f4b71Sopenharmony_ciAdd the following library to **CMakeLists.txt**: 25e41f4b71Sopenharmony_ci```txt 26e41f4b71Sopenharmony_cilibnative_vsync.so 27e41f4b71Sopenharmony_ci``` 28e41f4b71Sopenharmony_ci 29e41f4b71Sopenharmony_ci**Including Header Files** 30e41f4b71Sopenharmony_ci```c++ 31e41f4b71Sopenharmony_ci#include <native_vsync/native_vsync.h> 32e41f4b71Sopenharmony_ci``` 33e41f4b71Sopenharmony_ci 34e41f4b71Sopenharmony_ci1. Implement a VSync callback function. 35e41f4b71Sopenharmony_ci ```c++ 36e41f4b71Sopenharmony_ci #include <iostream> 37e41f4b71Sopenharmony_ci 38e41f4b71Sopenharmony_ci static bool flag = false; 39e41f4b71Sopenharmony_ci static void OnVSync(long long timestamp, void* data) 40e41f4b71Sopenharmony_ci { 41e41f4b71Sopenharmony_ci flag = true; 42e41f4b71Sopenharmony_ci std::cout << "OnVSync: " << timestamp << std::endl; 43e41f4b71Sopenharmony_ci } 44e41f4b71Sopenharmony_ci OH_NativeVSync_FrameCallback callback = OnVSync; // The callback function must be of the OH_NativeVSync_FrameCallback type. 45e41f4b71Sopenharmony_ci ``` 46e41f4b71Sopenharmony_ci2. Create an **OH_NativeVSync** instance. 47e41f4b71Sopenharmony_ci ```c++ 48e41f4b71Sopenharmony_ci char name[] = "hello_vsync"; 49e41f4b71Sopenharmony_ci OH_NativeVSync* nativeVSync = OH_NativeVSync_Create(name, strlen(name)); 50e41f4b71Sopenharmony_ci ``` 51e41f4b71Sopenharmony_ci 52e41f4b71Sopenharmony_ci3. Set the VSync callback function through the **OH_NativeVSync** instance. 53e41f4b71Sopenharmony_ci ```c++ 54e41f4b71Sopenharmony_ci #include <unistd.h> 55e41f4b71Sopenharmony_ci #include <iostream> 56e41f4b71Sopenharmony_ci 57e41f4b71Sopenharmony_ci OH_NativeVSync_RequestFrame(nativeVSync, callback, nullptr); 58e41f4b71Sopenharmony_ci while (!flag) { // Check the flag value. The while loop exits only after the VSync callback function is executed, indicating that a VSync signal is received. 59e41f4b71Sopenharmony_ci std::cout << "wait for vsync!\n"; 60e41f4b71Sopenharmony_ci sleep(1); 61e41f4b71Sopenharmony_ci } 62e41f4b71Sopenharmony_ci std::cout << "vsync come, end this thread\n"; 63e41f4b71Sopenharmony_ci ``` 64e41f4b71Sopenharmony_ci 65e41f4b71Sopenharmony_ci4. Destroy the **OH_NativeVSync** instance. 66e41f4b71Sopenharmony_ci ```c++ 67e41f4b71Sopenharmony_ci OH_NativeVSync_Destroy(nativeVSync); // Destroy the OH_NativeVSync instance when the application does not need to receive VSync signals. 68e41f4b71Sopenharmony_ci ``` 69