GithubHelp home page GithubHelp logo

summercoroutine's Introduction

SummerCoroutine

在阅读了vczh的文章后对协程(coroutine)的实现原理产生了一些兴趣,然而因为对其所提到的代码变换还不是太理解所以暂时只能通过较为简单的方式在C++中实现协程(三本我都考不上了...)。

基本上只是对Windows平台提供纤程做了一个简单的封装,上下文切换操作系统都帮忙干了...其他的平台也是类似,就不花时间了。

Example

#include "include/coroutine.h"
#include <iostream>
#include <string>

using namespace summer;
using namespace std;

void foo(string name, int start)
{
	for (auto i = 0; i < 5; ++i)
	{
		cout << name << ": " << (start + i) << endl;
		yield;
	}
}

int main()
{
	Scheduler scheduler;
	auto _1 = scheduler.CreateCoroutine(foo, "1", 1);
	auto _2 = scheduler.CreateCoroutine(foo, "2", 2);
	while (_1.resume() && _2.resume());
	return 0;
}

效果如下: test

大概稍稍了解了一点协程的实现原理,但是距离boost.coroutine或者libgo这些成熟的项目还有很长的距离。

summercoroutine's People

Contributors

locked-cat avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.