Giter Site home page Giter Site logo

georgyfirsov / w32coro Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 0.0 50 KB

C++14-compatible implementation of coroutines for Windows systems. Based of Win32 API fibers that are manually scheduled by this library.

License: GNU General Public License v3.0

C++ 98.80% C 1.20%
coroutines winapi cxx14 cpp14 library

w32coro's Introduction

w32coro

Small hand-written coroutines library. Written in C++14 with Win32 API (Fibers).

Examples

Receiving some information from server

#include "w32coro/w32coro.h"
#include "MyAwesomeProject.h"

int main()
{
    const auto ReceiveData = [](const std::wstring& wsServerName, DWORD dwPort) {
        IConnectionPtr spConn = Connect(wsServerName, dwPort);
        std::wstring wsData = spConn->Receive();
        
        w32coro::CoReturn(wsData);
    };
    
    std::wstring wsServerName = L"my.awesome.server";
    DWORD dwPort = 8080;
    
    const auto wsReceivedData = w32coro::CoAwait<std::wstring>(
        w32coro::Coroutine(ReceiveData, std::cref(wsServerName), dwPort));
        
    std::wcout << wsReceivedData << std::endl;
}

Suspending, resuming and returning various values

Actually this example can be extended to write real generator, that implements some methods such as begin and end to use it in range-based for loop.

#include "w32coro/w32coro.h"
#include "MyAwesomeProject.h"

int main()
{
    const auto Generate = [](size_t Initial) {
        while (true) {
            w32coro::CoYield(Initial++);
        }
    };
    
    w32coro::Coroutine gen(Generate, 0);
    
    std::vector<size_t> Numbers;
    for (size_t i = 0; i < 10; i++) {
        Numbers.emplace_back(gen.Get<size_t>());
        gen.Resume();
    }
    
    //
    // Will print 0 1 2 ...
    //
    for (size_t n : Numbers) {
        std::cout << n << ' ';
    }
}

w32coro's People

Contributors

georgyfirsov avatar

Watchers

 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.