使用c++对golang channel的实现
- 克隆到本地
git clone --depth 1 https://github.com/YangWithU/Chan_Cpp.git
- CMake 构建项目
mkdir build
cd build
cmake ..
- 使用 make 或 ninja进行编译
make
- 运行
./chan_cpp
- 基础 Go Channel 类型
- Buffered Channel
- 重载
<<
进行 Channel 对象存储和提取 - 基本 Select 语句
- 向 Channel 插入 Select 语句
- 乱序 Case 执行 Select 语句
- Channel 迭代器
- Close() 函数
Chan<int> ch;
// 插入
int i = 2;
ch << 1; //or
i >> ch;
// 提取
i << ch; //or
int y;
ch >> y
Chan<int, 5> multi;
thread([&]() {
multi << 1 << 2 << 3 << 4 << 5;
this_thread::sleep_for(chrono::milliseconds(500));
Close(multi);
}).detach();
for(auto& x : multi) {
cout << x << endl;
}
void fibonacci(Chan<int>& c, Chan<int>& quit)
{
int x=0, y = 1;
for (bool go = true; go;)
{
Select
{
Case{c << x,[&]()
{
int t = x;
x = y;
y += t;
}},
Case{quit,[&](auto v)
{
cout << "quit" << endl;
go = false;
}}
};
}
}
int main()
{
Chan<int> c;
Chan<int> quit;
thread([&]()
{
for (size_t i = 0; i < 10; i++)
{
cout << c << endl;
}
quit << 0;
}).detach();
fibonacci(c, quit);
}