boost 相关问题

Boost是一系列旨在用于C ++的高质量库。 Boost是免费的,通常被认为是“第二标准库”。

Boost Asio 同步与异步

我第一次使用 Boost Asio,我有一个接受多个连接的应用程序,每个连接都在单独的线程中,并将数据读/写到单个 TCP 套接字。 我对同步的理解遇到了...

回答 1 投票 0

为什么 cmake 找不到 boost 库的组件,还是我做了一些根本错误的事情?

我有一个给定的随机游走器实现。 我使用的是 Windows 10,通常,我更喜欢 Linux,但为此,我稍后可能需要我的 GPU,并且我不想在这台机器上进行双启动。 文档是

回答 1 投票 0

io_context 不会阻塞 async_connect

我正在尝试使用 boost asio 运行一个简单的客户端、服务器脚本。 我的 connect() 函数的代码如下,在我的 main.cpp 中调用一次。 无效连接(){ 如果(!连接) ...

回答 1 投票 0

如何使用 Boost::multi_index_container 检查枚举是否已在特定位上设置

我想知道是否可以修改以下代码片段以删除 rv::filter 并使用 boost::multi_index 代替。 我有一个包含名字、姓氏、工作职位和访问级别的结构体

回答 2 投票 0

Boost BZip2 未定义参考错误

链接测试时出现以下错误。我有一个静态库:coje,它链接包含的库,然后我的测试文件夹被通配,并为每个 .cpp 文件创建一个测试。我...

回答 1 投票 0

使用 Boost Asio 参考的分段错误

我用于asio代码的链接是https://www.boost.org/doc/libs/1_55_0/doc/html/boost_asio/tutorial/tutdaytime1.html #包括 #包括 #包括 我用于 asio 代码的链接是 https://www.boost.org/doc/libs/1_55_0/doc/html/boost_asio/tutorial/tutdaytime1.html #include <iostream> #include <boost/asio.hpp> #include <boost/array.hpp> // #include <src.hpp> using boost::asio::ip::tcp; int main(int argc, char *argv[]) { std::cout << "Started" << std::endl; try{ boost::asio::io_context io; boost::asio::steady_timer t(io, boost::asio::chrono::seconds(5)); tcp::resolver resolver(io); tcp::resolver::results_type endpoints = resolver.resolve(argv[1],"daytime"); tcp::socket socket(io); boost::asio::connect(socket,endpoints); boost::array<char,128> buf; boost::system::error_code error; size_t len = socket.read_some(boost::asio::buffer(buf), error); if (error == boost::asio::error::eof) { std::cout << "No Errors" << std::endl; } else if(error){ throw boost::system::system_error(error); } std::cout << "Data is: "; std::cout.write(buf.data(), len); io.run(); } catch(std::exception& e){ std::cerr << e.what() << std::endl; } return 0; } 我尝试为白天的 tcp 客户端运行代码时遇到分段错误,但我没有 知道为什么。它表示缓冲区功能自动防止参考上的缓冲区溢出。 我只是需要它不给我一个分段错误,我对 C++ 或网络都没有经验。 可能会出现段错误,因为您使用的命令行参数未传递? 对参数进行硬编码: tcp::resolver::results_type endpoints = resolver.resolve("127.0.0.1", "daytime"); 或者检查参数是否存在: if (argc < 2) { std::cerr << "Usage: " << argv[0] << " HOSTNAME" << std::endl; return 1; }

回答 1 投票 0

将 boost intrusive_ptr 与不同 const-ness 进行比较

我想将 boost::intrusive_ptr 实例与基础类型的不同 const 性进行比较,但不幸的是,如果您取消注释该行,它就无法从框中工作: #包括 我想将 boost::intrusive_ptr 实例与基础类型的不同常量进行比较,但不幸的是,如果您取消注释该行,则它不起作用: #include <boost/intrusive_ptr.hpp> #include <memory> struct S {}; void intrusive_ptr_release(const S*){} int main() { boost::intrusive_ptr<const S> boost_const_ptr; boost::intrusive_ptr<S> boost_ptr; // if(boost_const_ptr < boost_ptr) {} // <------ here const S* s_const_ptr; S* s_ptr; if(s_const_ptr < s_ptr) {} std::unique_ptr<const S> u_const_ptr; std::unique_ptr<S> u_ptr; if(u_const_ptr < u_ptr) {} } 错误将是: no match for 'operator<' (operand types are 'boost::intrusive_ptr<const S>' and 'boost::intrusive_ptr<S>') 我可以比较原始指针,甚至可以比较 std::unique_ptr,但不能比较 boost::intrusive_ptr。这是有原因的,还是只是疏忽? 一个可能的原因是,与 shared_ptr < would be impossible in general. Other smart pointer types don't have to deal with the possibility of heterogenous comparison1 具有相同的语义,但 shared_ptr、weak_ptr 和 intrusive_ptr 可以。 template<class T, class U> bool operator<(shared_ptr<T> const & a, shared_ptr<U> const & b) noexcept; 退货: 未指定的值使得 operator< 是严格的弱排序,如 C++ 标准的 [lib.alg.sorting] 部分所述; 在operator<、!(a < b) && !(b < a)定义的等价关系下,两个shared_ptr实例是等价的当且仅当它们 共有所有权或两者都是空的。 shared_ptr 可以通过比较引用计数的地址来做到这一点。 intrusive_ptr 不能这样做,因为它没有对引用计数的指针访问。 例如当您将 shared_ptr<Base> 与 shared_ptr<Derived> 进行比较时,它们可能共享同一对象的所有权,这对于 unique_ptr<Base> 和 unique_ptr<Derived> 来说是不可能的。

回答 1 投票 0

Boost 预处理器传递函数中的所有结构参数

是否可以使用 boost 预处理器 API 在函数中传递结构体的所有成员 我有类似的东西,但无法编译。 #包括 #包括 是否可以使用 boost 预处理器 API 在函数中传递结构体的所有成员 我有类似的东西,但无法编译。 #include <iostream> #include <boost/preprocessor/cat.hpp> #include <boost/preprocessor/seq/for_each.hpp> #include <boost/preprocessor/seq/variadic_seq_to_seq.hpp> #include <boost/preprocessor/seq/elem.hpp> // Define a macro to generate code for passing a struct member to a function #define PASS_MEMBER_TO_FUNCTION(r, structName, elem) \ function(BOOST_PP_SEQ_ELEM(0, elem), structName.BOOST_PP_SEQ_ELEM(1, elem)); // Define a macro to iterate over struct members and pass them to a function #define PASS_MEMBERS_TO_FUNCTION(structName, members) \ BOOST_PP_SEQ_FOR_EACH(PASS_MEMBER_TO_FUNCTION, structName, BOOST_PP_VARIADIC_SEQ_TO_SEQ(members)) // Example struct struct MyStruct { int height; double width; const char* name; }; // Example function that takes struct members void function(int value, double width, const char* name) { // Your function implementation here std::cout << "Value: " << value << ", Width: " << width << ", Name: " << name << std::endl; } int main() { // Usage example MyStruct myStruct{10, 20.5, "example"}; PASS_MEMBERS_TO_FUNCTION(myStruct, ((int, height))((double, width))((const char*, name))); return 0; } 使用 Boost PFR,您不需要预处理器: std::apply(function, boost::pfr::structure_tie(myStruct)); 演示 在 Coliru 上直播: #include <boost/pfr.hpp> #include <iostream> struct MyStruct { int height; double width; char const* name; }; // Example function that takes separate parameters void function(int value, double width, char const* name) { std::cout << "Value: " << value << ", Width: " << width << ", Name: " << name << std::endl; } int main() { // Usage example MyStruct myStruct{10, 20.5, "example"}; apply(function, boost::pfr::structure_tie(myStruct)); } 印刷 Value: 10, Width: 20.5, Name: example

回答 1 投票 0

C++ Boost kamada_kawai_spring_layout 问题

为了实现某些顶点位置固定的平面嵌入, 我目前正在使用力导向布局方法。这种做法可行吗? 我目前面临编译问题...

回答 1 投票 0

C++ G++ 错误:[-Werror=数组边界]

使用 make install 构建和安装 C++ 项目的二进制文件时,出现以下错误: > 编译算法/fba2-inst6.cpp [qkd_commsys_simulator-omp-mpi-gmp: 发布] > 在...

回答 1 投票 0

Boost 预处理器传递函数中的所有结构参数

是否可以使用 boost 预处理器 API 在函数中传递结构体的所有成员 我有类似的东西,但无法编译。 #包括 #包括 是否可以使用 boost 预处理器 API 在函数中传递结构体的所有成员 我有类似的东西,但无法编译。 #include <iostream> #include <boost/preprocessor/cat.hpp> #include <boost/preprocessor/seq/for_each.hpp> #include <boost/preprocessor/seq/variadic_seq_to_seq.hpp> #include <boost/preprocessor/seq/elem.hpp> // Define a macro to generate code for passing a struct member to a function #define PASS_MEMBER_TO_FUNCTION(r, structName, elem) \ function(BOOST_PP_SEQ_ELEM(0, elem), structName.BOOST_PP_SEQ_ELEM(1, elem)); // Define a macro to iterate over struct members and pass them to a function #define PASS_MEMBERS_TO_FUNCTION(structName, members) \ BOOST_PP_SEQ_FOR_EACH(PASS_MEMBER_TO_FUNCTION, structName, BOOST_PP_VARIADIC_SEQ_TO_SEQ(members)) // Example struct struct MyStruct { int height; double width; const char* name; }; // Example function that takes struct members void function(int value, double width, const char* name) { // Your function implementation here std::cout << "Value: " << value << ", Width: " << width << ", Name: " << name << std::endl; } int main() { // Usage example MyStruct myStruct{10, 20.5, "example"}; PASS_MEMBERS_TO_FUNCTION(myStruct, ((int, height))((double, width))((const char*, name))); return 0; } 使用 Boost PFR,您不需要预处理器: std::apply(function, boost::pfr::structure_tie(myStruct)); 演示 在 Coliru 上直播: #include <boost/pfr.hpp> #include <iostream> struct MyStruct { int height; double width; char const* name; }; // Example function that takes separate parameters void function(int value, double width, char const* name) { std::cout << "Value: " << value << ", Width: " << width << ", Name: " << name << std::endl; } int main() { // Usage example MyStruct myStruct{10, 20.5, "example"}; apply(function, boost::pfr::structure_tie(myStruct)); } 印刷 Value: 10, Width: 20.5, Name: example

回答 1 投票 0

增强对字符缓冲区的序列化和数据的反序列化

我正在使用 boost::interprocess::message_queue 在进程之间创建一个通信模块 因为消息队列占用数组缓冲区,所以我会将数据包序列化到数组缓冲区中。 内联空

回答 1 投票 0

使用 -Woverloaded-virtual 标志时的 GCC 错误

让我们看一下这段代码: #包括 结构类型:公共 boost::system::error_category { const char * name() const noexcept 覆盖 { 返回 nullptr;...

回答 1 投票 0

为什么 boost::multi_array 的 ConstMultiArrayConcept 有一个 NumDims 模板参数?

我写了一个运算符<< specialization that handles boost::multi_array, and used ConstMultiArrayConcept so that it would work on both the outer array and the sub-arrays. I'm wondering, though, ...

回答 2 投票 0

C++中函数的线性组合

假设我们有以下内容: // 一些函数 双 f1( 双 x ); 双 f2( 双 x ); 双 f3( 双 x ); // 系数 双 c1、c2、c3; // 输入变量 双x; 我们需要

回答 1 投票 0

升压信号如何断开信号?

正如boost_offical_web_site所说,boost::function没有可比性。但我对用法有疑问: #包括 B类{ 民众: 〜B(){ printf("B 释放:%p &

回答 1 投票 0

使用多个线程改变结果

我正在尝试利用多线程来优化 C++ 代码的运行时间。我尝试了几种不同的解决方案。 我有这个使用 boost 的代码: #包括 #包括 我正在尝试利用多线程来优化 C++ 代码的运行时间。我尝试了几种不同的解决方案。 我有这个使用 boost 的代码: #include <iostream> #include <vector> #include <chrono> #include <atomic> #include <thread> #include <boost/asio.hpp> #include <boost/bind/bind.hpp> void Test::boost_worker_task() { char new_state[3][3]; MyEngine::random_start_state(new_state); MyEngine::solve_game(new_state); ++games_solved_counter; } void Test::run(const unsigned int games_to_solve, const bool use_mul_thread) { MyEngine::init_rand(); const auto start = std::chrono::high_resolution_clock::now(); const unsigned int num_threads = use_mul_thread ? std::thread::hardware_concurrency() : 1; std::cout << "Using " << num_threads << " threads to solve " << games_to_solve << " games" << std::endl; boost::asio::io_service io_service; boost::asio::thread_pool pool(num_threads); for (unsigned int i = 0; i < games_to_solve; ++i) { io_service.post([] { return Test::boost_worker_task(); }); } // Run and wait for all tasks to complete io_service.run(); pool.join(); const auto end = std::chrono::high_resolution_clock::now(); const std::chrono::duration<double, std::milli> elapsed = end - start; std::cout << "Solved " << games_solved_counter << " games!" << std::endl; std::cout << "Elapsed time: " << elapsed.count() / 1000.0 << " seconds" << std::endl; std::cout << "Elapsed time: " << elapsed.count() << " milliseconds\n" << std::endl; } 还有这段代码: #include <iostream> #include <vector> #include <thread> #include <mutex> #include <chrono> #include <atomic> #include <future> std::atomic<int> games_solved(0); void TestMulThread::worker_task(const unsigned int num_iterations, std::mutex& games_solved_mutex) { for (unsigned int i = 0; i < num_iterations; ++i) { char new_state[3][3]; MyEngine::random_start_state(new_state); MyEngine::solve_game(new_state); ++games_solved; } } void TestMulThread::run(const unsigned int total_games_to_solve) { MyEngine::init_rand(); const auto start_time = std::chrono::high_resolution_clock::now(); const unsigned int num_threads = std::thread::hardware_concurrency(); const unsigned int games_per_thread = total_games_to_solve / num_threads; const unsigned int remaining_games = total_games_to_solve % games_per_thread; std::cout << "Using " << num_threads << " threads to solve " << total_games_to_solve << " games" << std::endl; // Distribute the remaining games std::vector<unsigned int> games_for_each_thread(num_threads, games_per_thread); for (unsigned int i = 0; i < remaining_games; ++i) { games_for_each_thread[i]++; } std::vector<std::future<void>> futures; std::mutex games_solved_mutex; for (unsigned int i = 0; i < num_threads; ++i) { futures.push_back(std::async(std::launch::async, worker_task, games_for_each_thread[i], std::ref(games_solved_mutex))); } for (auto& future : futures) { future.get(); } const auto end_time = std::chrono::high_resolution_clock::now(); const auto elapsed_time = std::chrono::duration_cast<std::chrono::milliseconds>(end_time - start_time).count(); std::cout << "Solved " << games_solved << " games!" << std::endl; std::cout << "Elapsed time: " << elapsed_time / 1000.0 << " seconds" << std::endl; std::cout << "Elapsed time: " << elapsed_time << " milliseconds\n" << std::endl; } 我的问题有两个: boost 版本的运行速度比第二个代码慢得多。它甚至比使用简单的 for 循环并且不尝试利用多个线程运行得更慢。我知道尝试并行运行任务可能会导致不同类型的开销,但第二个代码运行速度非常快,我想了解原因。 使用 Visual Studio 时,第二个代码片段运行得非常快(使用 Release 和 -O2 标志进行编译作为 C++ 优化),但是当我使用 g++ 在我的 Linux 机器上编译并运行相同的代码时,它的运行速度再次比使用 for 慢。循环运行相同数量的游戏。我尝试使用一些不同的设置进行编译,例如: g++ -O2 -o test Test.cpp -std=c++20 -lpthread g++ -O2 -o test Test.cpp -std=c++20 -pthread 对于为什么会出现这种情况有什么想法吗? 谢谢! 第一个程序从不使用线程池。除了加入。 您不知道/显示 hardware_concurrency 的值,因此您无法知道您正在将苹果与苹果进行比较。 如果至少修复第一个,则两者之间的性能匹配: 住在Coliru 文件test.h #pragma once #include <atomic> namespace Test { static void boost_worker_task(); static void run(unsigned, bool); static inline std::atomic_uint games_solved_counter{0}; }; // namespace Test namespace TestMulThread { static void worker_task(unsigned); static void run(unsigned); static inline std::atomic_uint games_solved{0}; }; // namespace TestMulThread struct MyEngine { using State = char[3][3]; static void init_rand(); static void random_start_state(State&); static void solve_game(State&); }; 文件test.cpp #include "test.h" #include <boost/asio.hpp> #include <iostream> #include <random> #include <thread> using namespace std::chrono_literals; static constexpr auto now = std::chrono::high_resolution_clock::now; void MyEngine::init_rand() {} void MyEngine::random_start_state(State&) {} void MyEngine::solve_game(State&) { thread_local static std::mt19937 prng{std::random_device{}()}; thread_local static std::uniform_int_distribution work(3, 8); std::this_thread::sleep_for(1ms * work(prng)); }; void TestMulThread::worker_task(unsigned num_iterations) { MyEngine::init_rand(); for (unsigned int i = 0; i < num_iterations; ++i) { char new_state[3][3]; MyEngine::random_start_state(new_state); MyEngine::solve_game(new_state); ++games_solved; } } void TestMulThread::run(unsigned total_games_to_solve) { auto start_time = now(); unsigned num_threads = std::thread::hardware_concurrency(); unsigned games_per_thread = total_games_to_solve / num_threads; unsigned remaining_games = total_games_to_solve % games_per_thread; std::cout << "Using " << num_threads << " threads to solve " << total_games_to_solve << " games" << std::endl; // Distribute the remaining games std::vector games_for_each_thread(num_threads, games_per_thread); for (unsigned int i = 0; i < remaining_games; ++i) { games_for_each_thread[i]++; } std::vector<std::future<void>> futures; for (unsigned i = 0; i < num_threads; ++i) futures.push_back(std::async(std::launch::async, worker_task, games_for_each_thread[i])); for (auto& future : futures) future.get(); auto elapsed_time = now() - start_time; std::cout << "Solved " << games_solved << " games!" << std::endl; std::cout << "Elapsed time: " << elapsed_time / 1.s << " seconds" << std::endl; std::cout << "Elapsed time: " << elapsed_time / 1ms << " milliseconds\n" << std::endl; } void Test::boost_worker_task() { char new_state[3][3]; MyEngine::random_start_state(new_state); MyEngine::solve_game(new_state); ++games_solved_counter; } void Test::run(unsigned games_to_solve, bool threaded) { auto const start = now(); unsigned num_threads = threaded ? std::thread::hardware_concurrency() : 1; std::cout << "Using " << num_threads << " threads to solve " << games_to_solve << " games" << std::endl; boost::asio::thread_pool pool(num_threads); for (unsigned i = 0; i < games_to_solve; ++i) post(pool, Test::boost_worker_task); // Run and wait for all tasks to complete pool.join(); auto elapsed = now() - start; std::cout << "Solved " << games_solved_counter << " games!" << std::endl; std::cout << "Elapsed time: " << elapsed / 1.s << " seconds" << std::endl; std::cout << "Elapsed time: " << elapsed / 1ms << " milliseconds\n" << std::endl; } int main() { // auto N = 10'000u; Test::run(N, true); TestMulThread::run(N); } 打印,在线: Using 4 threads to solve 10000 games Solved 10000 games! Elapsed time: 14.04 seconds Elapsed time: 14039 milliseconds Using 4 threads to solve 10000 games Solved 10000 games! Elapsed time: 14.1067 seconds Elapsed time: 14106 milliseconds 对我来说是本地的: Using 8 threads to solve 10000 games Solved 10000 games! Elapsed time: 6.99751 seconds Elapsed time: 6997 milliseconds Using 8 threads to solve 10000 games Solved 10000 games! Elapsed time: 7.03147 seconds Elapsed time: 7031 milliseconds

回答 1 投票 0

有什么优雅的方式来结合 asio::co_composed 和 std::variant 吗?

问题 我想处理具有相同签名成员函数的连接类。例如,tcp 和 tls 都是连接类。他们有 send() 成员函数模板,支持...

回答 1 投票 0

boost::describe:如何获得未修饰的枚举的名称?

Boost 描述可让您描述枚举并检查这些描述。我了解了如何获取枚举的成员,但是有没有办法获取枚举本身的名称? 类外层{ ... 枚举类

回答 1 投票 0

如何在使用 boost websocket_server_async 方法时使 aync_read 和 async_write 彼此独立?

我在我的项目中使用 boost websocket 并坚持使用这段代码,其中我的 async_write 仅当它从客户端读取某些内容时才执行。 我使用了 websocket_server_async 中的代码 下面...

回答 1 投票 0

© www.soinside.com 2019 - 2024. All rights reserved.