{"id":13730180,"url":"https://github.com/tonbit/coroutine","last_synced_at":"2025-05-08T02:32:04.859Z","repository":{"id":41361308,"uuid":"67801286","full_name":"tonbit/coroutine","owner":"tonbit","description":"C++11 single .h asymmetric  coroutine  implementation via ucontext / fiber","archived":false,"fork":false,"pushed_at":"2021-12-20T14:11:17.000Z","size":20,"stargazers_count":416,"open_issues_count":9,"forks_count":91,"subscribers_count":29,"default_branch":"master","last_synced_at":"2024-11-14T21:37:52.457Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/tonbit.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-09-09T13:29:53.000Z","updated_at":"2024-11-13T09:35:15.000Z","dependencies_parsed_at":"2022-09-16T08:21:00.476Z","dependency_job_id":null,"html_url":"https://github.com/tonbit/coroutine","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tonbit%2Fcoroutine","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tonbit%2Fcoroutine/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tonbit%2Fcoroutine/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tonbit%2Fcoroutine/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tonbit","download_url":"https://codeload.github.com/tonbit/coroutine/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252986899,"owners_count":21836250,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-03T02:01:11.062Z","updated_at":"2025-05-08T02:32:04.567Z","avatar_url":"https://github.com/tonbit.png","language":"C++","readme":"# C++11 single .h asymmetric coroutine implementation\n\n### API\n\nin namespace coroutine:        \n* routine_t create(std::function\u003cvoid()\u003e f);\n* void destroy(routine_t id);\n* int resume(routine_t id);\n* void yield();\n* TYPE await(TYPE(*f)());\n* routine_t current();\n* class Channel\u003cT\u003e with push()/pop();\n\n### OS\n\n* Linux\n* macOS\n* Windows\n\n### Demo\n\t\t\t\t\t\t\n```cpp\n#include \"coroutine.h\"\n#include \u003ciostream\u003e\n#include \u003cchrono\u003e\n\ncoroutine::Channel\u003cint\u003e channel;\n\nstring async_func()\n{\n    std::this_thread::sleep_for(std::chrono::milliseconds(3000));\n\treturn \"22\";\n}\n\nvoid routine_func1()\n{\n\tint i = channel.pop();\n\tstd::cout \u003c\u003c i \u003c\u003c std::endl;\n\t\n\ti = channel.pop();\n\tstd::cout \u003c\u003c i \u003c\u003c std::endl;\n}\n\nvoid routine_func2(int i)\n{\n\tstd::cout \u003c\u003c \"20\" \u003c\u003c std::endl;\n\tcoroutine::yield();\n\t\n\tstd::cout \u003c\u003c \"21\" \u003c\u003c std::endl;\n\n\t//run function async\n\t//yield current routine if result not returned\n\tstring str = coroutine::await(async_func);\n\tstd::cout \u003c\u003c str \u003c\u003c std::endl;\n}\n\nvoid thread_func()\n{\n\t//create routine with callback like std::function\u003cvoid()\u003e\n\tcoroutine::routine_t rt1 = coroutine::create(routine_func1);\n\tcoroutine::routine_t rt2 = coroutine::create(std::bind(routine_func2, 2));\n\t\n\tstd::cout \u003c\u003c \"00\" \u003c\u003c std::endl;\t\n\tcoroutine::resume(rt1);\n\n\tstd::cout \u003c\u003c \"01\" \u003c\u003c std::endl;\n\tcoroutine::resume(rt2);\n\t\n\tstd::cout \u003c\u003c \"02\" \u003c\u003c std::endl;\n\tchannel.push(10);\n\t\n\tstd::cout \u003c\u003c \"03\" \u003c\u003c std::endl;\n\tcoroutine::resume(rt2);\n\t\n\tstd::cout \u003c\u003c \"04\" \u003c\u003c std::endl;\n\tchannel.push(11);\n\t\n\tstd::cout \u003c\u003c \"05\" \u003c\u003c std::endl;\n\t\n\tstd::this_thread::sleep_for(std::chrono::milliseconds(6000));\n\tcoroutine::resume(rt2);\n\n\t//destroy routine, free resouce allocated\n\t//Warning: don't destroy routine by itself\n\tcoroutine::destroy(rt1);\n\tcoroutine::destroy(rt2);\n}\n\nint main()\n{\n\tstd::thread t1(thread_func);\n\tstd::thread t2([](){\n\t\t//unsupport coordinating routine crossing threads\n\t});\n\tt1.join();\n\tt2.join();\n\treturn 0;\n}\n```","funding_links":[],"categories":["C++"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftonbit%2Fcoroutine","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftonbit%2Fcoroutine","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftonbit%2Fcoroutine/lists"}