Skip to content

YangWithU/Chan_Cpp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Chan_Cpp

使用c++对golang channel的实现

编译运行

  1. 克隆到本地
git clone --depth 1 https://github.com/YangWithU/Chan_Cpp.git
  1. CMake 构建项目
mkdir build
cd build
cmake ..
  1. 使用 make 或 ninja进行编译
make
  1. 运行
./chan_cpp

features

  • 基础 Go Channel 类型
  • Buffered Channel
  • 重载 << 进行 Channel 对象存储和提取
  • 基本 Select 语句
  • 向 Channel 插入 Select 语句
  • 乱序 Case 执行 Select 语句
  • Channel 迭代器
  • Close() 函数

examples

无缓冲 Channel

Chan<int> ch;
// 插入
int i = 2;
ch << 1; //or
i >> ch;
// 提取
i << ch; //or
int y;
ch >> y

有缓冲 Channel

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;
}

Select

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);
}

About

使用c++对golang channel的实现

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published