future 相关问题

计算结束前计算结果的占位符。用于并发编程。关于未来事件的问题在Stack Overflow上是偏离主题的。

将值从 Future 映射到对象而不是 Future[Object]

我有一个返回 Future[State] 的函数和另一个我需要返回 StateSimplified 对象的函数 //类状态 案例类状态(id:字符串,标签:字符串,邮政编码:字符串,扩展名:

回答 1 投票 0

在 Flutter 的 FutureBuilder 中检查 `snapshot.hasData` 和 `snapshot.data != null` 是否多余?

我正在使用 Flutter 的 FutureBuilder,并发现了一种模式,在渲染小部件之前会检查 snapshot.hasData 和 snapshot.data != null: 未来建造者( 未来:未来, 布...

回答 2 投票 0

clojure 未来与延迟

我一直在阅读《Programming in Clojure》并发现了文本 (defn 获取文档 [id] ; ...做一些工作来检索已识别文档的元数据... {:url "http://www.mozilla....

回答 2 投票 0

库币期货Python API,Kline获取数据不起作用

我正在尝试获取比特币期货市场(符号为“.KXBTUSDT”)特定时间(2022 年 4 月 4 日)的 Klines 历史数据。然而,当我调用该函数时,API 返回...

回答 3 投票 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

future 无法在线程之间安全地发送

我想通过from_request实现jwt,但是出现错误,说Send没有实现。然而,如果删除结构的特征实现,错误就会消失(即:...

回答 1 投票 0

为什么执行器服务提交的任务没有异步执行任务?

我正在尝试在我的 intellij 本地中使用异步 java。我的意图是,我将从 main 方法调用 calucalateAsync() 方法,然后将调试点放在 System.out.println("calculate

回答 1 投票 0

如何从 boost::asio::post 获得未来?

我正在使用Boost 1.66.0,其中asio内置了与futures互操作的支持(并且已经有一段时间了)。我在网上看到的例子表明了如何在使用时干净地实现这一点

回答 3 投票 0

如何从 Future 函数返回这个布尔值?

我有一个函数正在监听文档中的更改以确定用户是否在线或离线,但我无法返回布尔值并且返回默认值...

回答 1 投票 0

在 Rust 中实现异步构建器模式

我想为建筑商实现一个未来。这对于例如通过构建器配置请求然后将其发送出去的示例来说很常见。 我想为构建器实现 IntoFuture,但我不能...

回答 1 投票 0

多个线程可以同时复制一个std::shared_future吗?

GetNextImage() 中的shared_lock 是否足够,还是需要使用unique_lock? 共享_future shfutNextImage; 共享互斥体shmutNextImage; 共享_future GetNextImage() {

回答 1 投票 0

异步关键字Future<dynamic>问题。我只是想知道下面的代码是如何生成错误的

我不知道这部分代码发生了什么: 未来 fnct() { print("fnct() 内部"); return Future.delayed(Duration(秒:4),()=>“你好”); } 未来的 fnct2() ...

回答 1 投票 0

从 T 创建 std::future<T> 的最佳方式

我正在使用 std::future 来存储可选异步操作的结果。根据函数的参数,操作可以是异步的,也可以是同步的。在同步中...

回答 3 投票 0

executor.map 和命名参数传递

我有一个函数必须使用大量参数类型运行的情况 # 带有命名参数的泛型函数 def genericFn(X, y, a_fn ='t', o_fn ='a', l_fn='b', dim=2): ...

回答 1 投票 0

Flutter/Dart 未来

类 _WorterBuchState 使用 TickerProviderStateMixin 扩展 State { var wordPair= '' ; @覆盖 无效的初始化状态(){ super.initState(); 加载(); } 未来...

回答 2 投票 0

我有两个可排队作业在事务中运行如何解决它

在订单产品对象上,我与第 3 方系统集成,每当通过 Queueable 类订购产品时,该系统都会向第 3 方发送数据。 现在我正在尝试将一组记录插入到

回答 1 投票 0

强制 QiChatbot 回复用户输入的短语

我正在 Android 上开发 Pepper 应用程序,该应用程序使用 QiChatbot 与用户就矿物进行交流。我想让聊天机器人也可供有听力障碍的人在屏幕上使用...

回答 1 投票 0

Python 中 future/promise(线程)的等价物是什么?

我来自C++世界,我正在寻找Python中std::future、std::promise的等价物。 Python 中是否有等效的机制或其他方法来实现相同的目的? 我知道

回答 2 投票 0

我如何从类扩展ChangeNotifier中获取价值

如何从类扩展 ChangeNotifier 中获取值 当我使用 User user = Provider.of(context, Listen: false).user; => 用户为空? UserController 类扩展了 ChangeNot...

回答 1 投票 0

使用 java 7 未来改造 2

有没有办法将retrofit 2与java 7 futures(而不是java 8 completablefutures)一起使用? 类似的东西 接口我的服务{ GET(“某条路径”) 未来 getUser() }

回答 1 投票 0

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