Stories

Detail Return Return

Coke(一):優秀的C++ Workflow支持協程啦 - Stories Detail

Coke(一):你好,世界

C++ Workflow是一款高性能的異步編程範式,自Github開源以來,已經收貨了一萬多枚Star,得到了越來越多的認可。

Coke項目是一個高性能的協程庫,基於C++ 20提供的協程組件開發,提供一組簡潔的異步接口,而其後台則是由C++ Workflow強力驅動。使用Coke可以輕鬆地創建協程任務,並通過C++ Workflow的調度器高效地調度和執行,Coke希望為大家帶來一種新穎的體驗C++ Workflow的方式。

作為開篇,本文介紹大家進入編程世界常遇到的第一個示例:Hello World

#include <iostream>
#include <chrono>
#include "coke/coke.h"

coke::Task<> helloworld(size_t n, std::chrono::milliseconds ms) {
    for (size_t i = 0; i < n; i++) {
        // 每兩次打印之間等待500毫秒,與同步等待不同,異步等待不會阻塞任何線程
        if (i != 0)
            co_await coke::sleep(ms);

        // 向屏幕打印 Hello World
        std::cout << "Hello World" << std::endl;
    }
}

int main() {
    // 啓動協程任務,並等待其完成
    coke::sync_wait(helloworld(3, std::chrono::milliseconds(500)));
    return 0;
}

Coke項目正在開發中,有想法或建議的同學歡迎提Issue或Pull request!

本系列文章同步發佈於個人網站和知乎專欄,轉載請註明來源,以便讀者及時獲取最新內容及勘誤。

user avatar lamazhenyuan Avatar donnytab Avatar youqingyouyidedalianmao Avatar muzijun_68c14af5563a2 Avatar jkkang Avatar wunima Avatar liu_chen Avatar ishare Avatar hunter_58d48c41761b8 Avatar keen_626105e1ef632 Avatar hantianfeng Avatar thinkerdjx Avatar
Favorites 14 users favorite the story!
Favorites

Add a new Comments

Some HTML is okay.