Giter Site home page Giter Site logo

libfly's Introduction

libfly

libfly协程库,采用C++作为编程语言,封装了协程类,并提供了协程基本操作和调度功能

一个简单的生产者-消费者的协程实现

using namespace libfly;

Conditional_variable cond;
int product;

void *timerCallback(Routine * routine)
{
    routine->resume();
}

void * Producer(void *arg)
{
    while(1)
    {
        product ++;
        product ++;
        product ++;
        std::cout<<"Producer  product::"<<product<<std::endl;
        cond.notify_one();
        //返回主协程
        Poller(timerCallback , get_curr_routine(),1); 
    }
}

void * Consumer(void *arg)
{
    while(1)
    {

        if(product == 0)
            cond.wait();
        product--;
        std::cout<<"Consumer  product::"<<product<<std::endl;
    }
}

int main()
{
    
    std::vector<Routine*> RoutineArr; 
    RoutineArr.push_back(new Routine(get_curr_thread_env(),NULL,Consumer,NULL) );	
    RoutineArr.push_back(new Routine(get_curr_thread_env(),NULL,Producer,NULL) );	
    for(int i=0;i<2;i++)
    {
        RoutineArr[i]->resume();
    }
    std::cout<<"I'm Main routine"<<std::endl; 
    EventLoop eventloop(get_curr_thread_env()->time_heap_,NULL,NULL);
    eventloop.loop();
    return 0;
}

libfly's People

Contributors

chudongfang avatar

Watchers

skyformat99 avatar  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.