1
0
mirror of synced 2025-11-06 03:20:55 +08:00
Files
SnowFlake-IdGenerator/C++/README.md
2024-02-05 14:59:06 +08:00

50 lines
1.7 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# ❄ idgenerator-C++
## 编译说明
跨平台只需要该系统有C++11或以上编译器就可以编译使用。
## 使用方式
只需要包含 IdGenerator.h 头文件即可
#include "idgen/IdGenerator.h"
## 调用示例CPP
第1步**全局** 初始化(应用程序启动时执行一次,实际上代码保证了初始化只能运行一次):
```c
// 初始化方式1
idgen::IdGeneratorOptions options;
options.Method = method;
options.WorkerId = 1; // Your_Unique_Worker_Id
options.SeqBitLength = 10; // 默认值6限制每毫秒生成的ID个数。若生成速度超过5万个/秒,建议加大 SeqBitLength 到 10。
// options.WorkerIdBitLength = 10; // 默认值6限定 WorkerId 最大值为2^6-1即默认最多支持64个节点。
// options.BaseTime = Your_Base_Time; // 如果要兼容老系统的雪花算法此处应设置为老系统的BaseTime。
idgen::IdGenerator::CreateInstance(options);
// 初始化方式2:
// 或者如果使用默认的options参数, 只需要
//idgen::IdGenerator::CreateInstance(Your_Unique_Worker_Id);
// 以上过程只需全局一次且应在生成ID之前完成。
```
第2步生成ID
```c
// 初始化后在任何需要生成ID的地方调用以下方法
long newId = idgen::IdGenerator::NextId();
```
## 编译示例CPP
```shell
如果c++编译器默认是c++11以前的但支持c++11, 编译的时候需要加上 -std=c++11 类似:
g++ -std=c++11 -pthread -g -W -O2 main.cpp -o testcpp
如果c++编译器默认就是c++11或以上的只需类似
g++ -pthread -g -W -O2 main.cpp -o testcpp
编译完毕后,可以运行,例如:
./testcpp > testcpp.log
```