C ++中的跨平台Coroutine库。

| 目标系统 | 工具链 | 笔记 |
|---|---|---|
| Linux | 海湾合作委员会 | 静态链接 |
| Linux | 海湾合作委员会 | 动态链接 |
| Linux | GCC-Latest | |
| Linux | GCC-Latest | 不例外 |
| Linux | GCC-Latest | 线程不安全 |
| Linux | GCC 4.8 | 遗产 |
| Linux | 最叮当 | 使用libc ++ |
| mingw64 | 海湾合作委员会 | 动态链接 |
| 视窗 | Visual Studio 2019 | 静态链接 |
| 视窗 | Visual Studio 2019 | 动态链接 |
| 视窗 | Visual Studio 2017 | 遗产,静态链接 |
| macos | appleclang | 使用libc ++ |
根据MIT许可证的许可证
可以在https://libcopp.atframe.work上找到文档,API参考CANBE在https://libcopp.atframe.work/doxygen/html/。
using value_type = int;使用cotask::task<T>中的T 。T cotask::task<T>重命名stack_allocator_t到stack_allocator_type 。cotask::task<T>将coroutine_t重命名为T中的coroutine_type 。libcopp::util::*到copp::util:: 。libcopp::util::intrusive_ptr<cotask::impl::task_impl>现在,请改用cotask::task<T>::ptr_type 。 cotask::task::await cotask::task::await_taskcotask::task<TCO_MACRO, TTASK_MACRO>用cotask::task<TCO_MACRO> ,我们现在不允许自定义ID分配器。cotask::core::standard_int_id_allocator<uint64_t>用copp::util::uint64_id_allocator ,我们现在不允许现在自定义ID分配器。ar, as, ld (Binutils)或LLVMgit clone https://github.com/Microsoft/vcpkg.git
cd vcpkg
PS > bootstrap-vcpkg.bootstrap
Linux: ~ /$ ./bootstrap-vcpkg.shPS > . v cpkg install libcopp [--triplet x64-windows-static/x64-windows/x64-windows-static-md and etc...]
Linux: ~ /$ ./vcpkg install libcopp请参阅:参考:`与cmake <用法使用一起使用cmake>`在下面的cmake中使用。
git clone --single-branch --depth=1 -b master https://github.com/owent/libcopp.git
mkdir libcopp/build && cd libcopp/build # cmake <libcopp dir> [options...]
cmake .. -DPROJECT_ENABLE_UNITTEST=YES -DPROJECT_ENABLE_SAMPLE=YEScmake --build . --config RelWithDebInfo # or make [options] when using Makefiletest/sample/benchmark [可选] # Run test => Required: PROJECT_ENABLE_UNITTEST=YES
ctest -VV . -C RelWithDebInfo -L libcopp.unit_test
# Run sample => Required: PROJECT_ENABLE_SAMPLE=YES
ctest -VV . -C RelWithDebInfo -L libcopp.sample
# Run benchmark => Required: PROJECT_ENABLE_SAMPLE=YES
ctest -VV . -C RelWithDebInfo -L libcopp.benchmarkcmake --build . --config RelWithDebInfo --target install # or make install when using Makefile然后只需包含并链接libcopp.*/libcotask.* ,或参见:ref:`与cmake <使用cmake-usage-using-make> make>`对于下面的cmake。
选项可以是CMAKE选项。例如控制构建操作的LIBCOPP的设置编译工具链,源目录或选项。 LIBCOPP选项如下:
| 选项 | 描述 |
|---|---|
| build_shared_libs = yes |否 | [default = no]构建动态库。 |
| libcopp_enable_seginged_stacks = yes |否 | [default = no]启用拆分堆栈支持的上下文。 |
| libcopp_enable_valgrind = yes |否 | [default = yes]启用valgrind支持的上下文。 |
| project_enable_unittest = yes |否 | [默认值=否]构建单元测试。 |
| project_enable_sample = yes |否 | [默认值= no]构建样本。 |
| libcopp_lock_disable_this_mt = yes |否 | [default = no]禁用对copp::this_coroutine和cotask::this_task多线程支持。 |
| libcopp_disable_atomic_lock = yes |否 | [default = no]禁用多线程支持。 |
| libcotask_enable = yes |否 | [default = yes]启用构建libcotask。 |
| libcopp_fcontext_use_tsx = yes |否 | [default = yes]启用英特尔交易同步扩展(TSX)。 |
| libcopp_macro_tls_stack_protector = yes |否 | [default = no]用户需要设置libcopp_macro_tls_stack_protector = on用-fstack-protector编译时。因为它更改了默认上下文切换逻辑。 |
| gtest_root = [路径] | 设置GTEST库安装前缀路径 |
| boost_root = [路径] | 设置boost.test库安装前缀路径 |
set(Libcopp_ROOT <where to find libcopp/INSTALL_PREFIX>)${CUSTOM_TARGET_NAME}中) find_package (Libcopp CONFIG REQUIRED)
target_link_libraries ( ${CUSTOM_TARGET_NAME} libcopp::cotask)
# Or just using copp by target_link_libraries(${CUSTOM_TARGET_NAME} libcopp::copp)如果使用MSVC和VCPKG,则CRT必须与VCPKG的三重态匹配,以下这些代码可能很有帮助:
if ( MSVC AND VCPKG_TOOLCHAIN)
if ( DEFINED ENV{VCPKG_DEFAULT_TRIPLET} AND NOT DEFINED VCPKG_TARGET_TRIPLET)
set (VCPKG_TARGET_TRIPLET " $ENV{VCPKG_DEFAULT_TRIPLET} " CACHE STRING "" )
endif ()
if (VCPKG_TARGET_TRIPLET MATCHES "^.*windows-static$" )
set (CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>" CACHE STRING "" )
else ()
set (CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>DLL" CACHE STRING "" )
endif ()
endif ()在https://github.com/microsoft/vcpkg/tree/master/master/ports/libcopp上查看更多详细信息。
只需包括标题和链接平台的库文件以使用libcopp即可。
LIBCOPP_PREFIX= < WHERE TO INSTALL libcopp >
# Example command for build sample with gcc 4.9 or upper on Linux
for source in sample_readme_ * .cpp ; do
g++ -std=c++14 -O2 -g -ggdb -Wall -Werror -fPIC -rdynamic -fdiagnostics-color=auto -Wno-unused-local-typedefs
-I $LIBCOPP_PREFIX /include -L $LIBCOPP_PREFIX /lib64 -lcopp -lcotask $source -o $source .exe ;
done
# Example command for build sample with clang 3.9 or upper and libc++ on Linux
for source in sample_readme_ * .cpp ; do
clang++ -std=c++17 -stdlib=libc++ -O2 -g -ggdb -Wall -Werror -fPIC -rdynamic
-I $LIBCOPP_PREFIX /include -L $LIBCOPP_PREFIX /lib64 -lcopp -lcotask -lc++ -lc++abi
$source -o $source .exe ;
done
# AppleClang on macOS just like those scripts upper.
# If you are using MinGW on Windows, it's better to add -static-libstdc++ -static-libgcc to
# use static linking and other scripts are just like those on Linux. # Example command for build sample with MSVC 1914 or upper on Windows & powershell(Debug Mode /MDd)
foreach ( $source in Get-ChildItem -File -Name . s ample_readme_ * .cpp) {
cl /nologo /MP /W4 /wd " 4100 " /wd " 4125 " /EHsc /std:c++17 /Zc:__cplusplus /O2 /MDd /I $LIBCOPP_PREFIX /include $LIBCOPP_PREFIX /lib64/copp.lib $LIBCOPP_PREFIX /lib64/cotask.lib $source
}服务器示例用于使用copp::coroutine_context , copp::coroutine_context_fiber和cotask::task :
task::then或task::await_taskcopp::callable_promisecopp::generator_future for C ++ 20 Coroutinecotask::taskcotask::task在Windows上使用Windows Fiber和SetUnhandledExceptionFilter所有示例代码均可在:ref:`示例<示例_doc_anchor>` and sample。
拆分堆栈支持:如果在Linux和用户GCC 4.7.0或upper中,请添加-DLIBCOPP_ENABLE_SEGMENTED_STACKS=YES使用拆分堆栈支持的上下文。
建议使用堆栈池代替GCC拆分堆栈。
请参阅CI输出以获取最新的基准报告。点击访问GitHub动作。
问:如何启用C ++ 20 Coroutine
/std:c++latest /await MSVC 1932及以下或以下或-std=c++20 -fcoroutines-ts -stdlib=libc++ clang 13及以下或以下或以下或以下或-std=c++20 -fcoroutines for GCC 10。如果您只能使用-std=c++20 -stdlib=libc++ clang 14或更高,则为GCC 11或更高版本-astd=c++20 ,和/std:c++latest MSVC 1932或更新。
问:libcopp会处理异常吗?
问:为什么SetUnhandledExceptionFilter无法在coroutine中捕获未经治疗的异常?
SetUnhandledExceptionFilter仅与Windows光纤一起使用,请参阅sample/sample_readme_11.cpp有关详细信息。 如果您有任何疑问,请创建一个问题并提供您的环境信息。例如:
cmake .. -G "Visual Studio 16 2019" -A x64 -DLIBCOPP_FCONTEXT_USE_TSX=ON -DPROJECT_ENABLE_UNITTEST=ON -DPROJECT_ENABLE_SAMPLE=ON-DCMAKE_BUILD_TYPE=RelWithDebInfo -DCMAKE_INSTALL_PREFIX=%cd%/install-prefix / cmake .. -G Ninja -DLIBCOPP_FCONTEXT_USE_TSX=ON -DPROJECT_ENABLE_UNITTEST=ON -DPROJECT_ENABLE_SAMPLE=ON -DCMAKE_BUILD_TYPE=RelWithDebInfo -DCMAKE_INSTALL_PREFIX=/opt/libcoppcmake --build . -jCXX所有将CC CMAKE工具链AR环境变量