GithubHelp home page GithubHelp logo

ve3wwg / coroutines_boost_0_53 Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 120 KB

Coroutine framework based upon Boost_0_53 (CentOS7) boost::context

License: GNU General Public License v2.0

Makefile 2.37% C++ 97.63%

coroutines_boost_0_53's Introduction

Coroutine Class using Boost 1.53

Requires boost_context 1.53.0 library.

CoroutineMain:

supplies the context for a main thread, already owning
a thread (line a main() program).

Coroutine:

is used for a new context, requiring a stack
to be allocated.

Coroutine co1(myfunc[,stack_size]);

When stack_size is not supplied, a minimum stack is
allocated.

CoroutineBase:

class from which Coroutine and CoroutineMain classes
are derived.

CoroutineBase::yield(CoroutineBase& co)

This method transfers control from the current coroutine
(or main thread) to the new coroutine.

CoroutineBase::fun_t

This is the function pointer to the code to be run as
a coroutine. It is passed the CoroutineBase pointer 
of the executing coroutine (either Coroutine or 
CoroutineMain class). The function must return its
Coroutine class pointer. 

Control is yielded by specifying the Coroutine class
to pass control to.

To pass data, derive a class from Coroutine or Coroutine
main as required.

CoroutineBase *fun1(CoroutineBase *co) {

    co->yield(...);
    ...
    return co;
}

Starting a coroutine from main:

CoroutineMain mco;
    
int main(int argc,char **argv) {
    Coroutine co1(fun1);

    mco.yield(co1);

Build:

make [BOOST=$HOME/local]

./coroutine # test program

Test Example:

#include "coroutine.hpp"

CoroutineMain mco;
Coroutine *cr1, *cr2;

static CoroutineBase *
fun1(CoroutineBase *co) {
	int x=10;

	std::cout << "fun1::a x=" << x++ << '\n';
	co->yield(*cr2);

	std::cout << "fun1::b x=" << x++ << '\n';
	co->yield(*cr2);

	return co;
}

static CoroutineBase *
fun2(CoroutineBase *co) {
	int x=20;

	std::cout << "fun2::a x=" << x++ << '\n';
	co->yield(mco);

	std::cout << "fun2::b x=" << x++ << '\n';
	co->yield(mco);

	return co;
}

int
main(int argc,char **argv) {
	Coroutine co1(fun1), co2(fun2);

	cr1 = &co1;
	cr2 = &co2;
	mco.yield(co1);
	std::cout << "Back to main a..\n";
	mco.yield(co1);
	std::cout << "Back to main b..\n";
	return 0;
}

Example Output:

$ ./coroutine 
fun1::a x=10
fun2::a x=20
Back to main a..
fun1::b x=11
fun2::b x=21
Back to main b..

Server Example:

$ ./server 

Will cause it to listen to 127.0.0.1:2345

Client Test:

$ wget http://127.0.0.1:2345/whatever/params?pig=oink --save-headers -qO-
HTTP/1.1 200 OK 
Connection: Keep-Alive
Content-Length: 209

Request type: GET
Request path: /whatever/params?pig=oink
Http Version: HTTP/1.1
Request Headers were:
Hdr: HOST: 127.0.0.1:2345
Hdr: CONNECTION: Keep-Alive
Hdr: ACCEPT: */*
Hdr: USER-AGENT: Wget/1.16
$ 

coroutines_boost_0_53's People

Contributors

ve3wwg avatar

Watchers

 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.