CO 2.0.1 发布,增加更多 golang 特性

栏目: 软件资讯 · 发布时间: 4年前

内容简介:GitHub https://github.com/idealvin/co 参考文档 中文: [github](https://idealvin.github.io/cn/about/co/) [gitee](https://idealvin.gitee.io/cn/about/co/) English: [github](https://idealvin.github.io/...

GitHub

参考文档

  • 中文: [github](https://idealvin.github.io/cn/about/co/) [gitee](https://idealvin.gitee.io/cn/about/co/)

  • English: [github](https://idealvin.github.io/en/about/co/) [gitee](https://idealvin.gitee.io/en/about/co/)

新特性

  • xrepo
sh
xrepo install -f "openssl=true,libcurl=true" co

 

  • vcpkg
sh
vcpkg install co:x64-windows
 
# http & ssl support
vcpkg install co[libcurl,openssl]:x64-windows

 

  • defer (类似于 golang 中的 defer)
cpp
#include "co/defer.h"
Timer t;
defer(LOG << "time elapse: " << t.us() << "us");

 

  • channel (类似于 golang 中的 channel)
cpp
#include "co/co.h"
 
DEF_main(argc, argv) {
    co::Chan<int> ch;
    go([ch]() {
        ch << 7;
    });
 
    int v = 0;
    ch >> v;
    LOG << "v: "<< v;
 
    return 0;
}

 

  • waitgroup (类似于 golang 中的 sync.WaitGroup)
cpp
#include "co/co.h"
 
DEF_main(argc, argv) {
    FLG_cout = true;
 
    co::WaitGroup wg;
    wg.add(8);
 
    for (int i = 0; i <8; ++i) {
        go([wg]() {
            LOG << "co: "<< co::coroutine_id();
            wg.done();
        });
    }
 
    wg.wait();
    return 0;
}

 

  • 协程在 windows 平台支持 hook。
  • 在指定调度线程中创建协程。
cpp
auto s = co::next_scheduler();
s->go(f1);
s->go(f2);
  • 在所有调度线程中创建协程。
cpp
auto& s = co::all_schedulers();
for (size_t i = 0; i <s.size(); ++i) {
    s[i]->go(f);
}
  • 增加 `void flag::init(const fastring& path);`

改变

  • 全局的 `Closure` 改为 `co::Closure`.
  • 改进 `co::Event`, 可以在协程及非协程中使用, 支持拷贝构造、lambda 中按值捕获.
  • 改进 `co::Mutex``co::Pool`, 支持拷贝构造、lambda 中按值捕获.
  • 改进 `co::close()`, 可以在任何地方调用.
  • 部分支持 mingw, 协程相关特性暂时不能在 mingw 上运行.

问题修复

  •  修复 `fs::file` 读写超过 4G 长度数据的 bug.
  •  修复 http::Client 连接超时时的错误信息.
  •  修复 #165 中的链接问题.

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Beautiful Code

Beautiful Code

Greg Wilson、Andy Oram / O'Reilly Media / 2007-7-6 / GBP 35.99

In this unique work, leading computer scientists discuss how they found unusual, carefully designed solutions to difficult problems. This book lets the reader look over the shoulder of major coding an......一起来看看 《Beautiful Code》 这本书的介绍吧!

在线进制转换器
在线进制转换器

各进制数互转换器

SHA 加密
SHA 加密

SHA 加密工具

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器