这是我在 boost 文档中某处找到的关于如何生成工作线程以与纤维 work_stealing 算法一起使用的示例的完整版本。
#include <iostream>
#include <chrono>
#include <boost/fiber/all.hpp>
int main() {
size_t count = std::thread::hardware_concurrency();
boost::fibers::barrier b{count};
for(int i=0;i<count;i++) {
new std::thread([&b, &count] {
boost::fibers::use_scheduling_algorithm<boost::fibers::algo::work_stealing>(count);
b.wait();
});
}
std::this_thread::sleep_for(std::chrono::seconds(5));
return 0;
}
这在大多数情况下都会导致段错误,我不明白为什么。
这里是cmake文件:
cmake_minimum_required(VERSION 3.12)
set(CMAKE_CXX_STANDARD 17)
set(VERSION 1_68_0)
set(BOOST_ROOT /home/User/boost_${VERSION})
find_package(Boost REQUIRED COMPONENTS fiber)
find_package(Threads)
include_directories(${Boost_INCLUDE_DIRS})
add_executable(test test.cpp)
target_link_libraries(test ${Boost_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT})
我正在运行 Fedora 28 并使用 gcc 8.1.1 从源代码构建了 Boost,但没有安装它。该项目是使用相同的编译器构建的。(libc++ 没有安装在任何地方。)我得到与 git 分支 master 和 development 以及 1_67_0 相同的行为。我觉得我在这里遗漏了一些明显的东西。