- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我对 C++ 和一般编程还很陌生。为了练习,我做了一个类似mergesort的排序算法。然后我尝试让它成为多线程。
std::future<T*> first = std::async(std::launch::async, &mergesort, temp1, temp1size);
std::future<T*> second = std::async(std::launch::async, &mergesort, temp2, temp2size);
temp1 = first.get();
temp2 = second.get();
但我的编译器似乎无法决定使用哪个模板,因为我两次遇到相同的错误。
Error 1 error C2783: 'std::future<result_of<enable_if<std::_Is_launch_type<_Fty>::value,_Fty>::type(_ArgTypes...)>::type> std::async(_Policy_type,_Fty &&,_ArgTypes &&...)' : could not deduce template argument for '_Fty'
Error 2 error C2784: 'std::future<result_of<enable_if<!std::_Is_launch_type<decay<_Ty>::type>::value,_Fty>::type(_ArgTypes...)>::type> std::async(_Fty &&,_ArgTypes &&...)' : could not deduce template argument for '_Fty &&' from 'std::launch'
这些错误让我相信 std::async 被两个不同的模板重载,一个用于指定的策略,一个用于未指定的策略,编译器无法选择正确的模板(我使用的是 Visual Studio Express 2013 ).那么如何向编译器指定合适的模板呢? (做 std::future<T*> second = std::async<std::launch::async>(&mergesort, temp2, temp2size);
似乎不起作用,我得到无效的模板参数,预期类型)。有没有更好的方法来一起完成这一切?谢谢!
最佳答案
您需要为mergesort
指定模板参数。异步不会足够聪明,无法自行解决。下面显示了一个基于迭代器的示例。它还利用当前事件线程作为递归点,而不是燃烧等待两个其他线程的线程句柄。
我警告你,有更好的方法可以做到这一点,但调整它可能会满足你的需求。
#include <iostream>
#include <algorithm>
#include <vector>
#include <thread>
#include <future>
#include <random>
#include <atomic>
static std::atomic_uint_fast64_t n_threads = ATOMIC_VAR_INIT(0);
template<typename Iter>
void mergesort(Iter begin, Iter end)
{
auto len = std::distance(begin,end);
if (len <= 16*1024) // 16K segments defer to std::sort
{
std::sort(begin,end);
return;
}
Iter mid = std::next(begin,len/2);
// start lower parttion async
auto ft = std::async(std::launch::async, mergesort<Iter>, begin, mid);
++n_threads;
// use this thread for the high-parition.
mergesort(mid, end);
// wait on results, then merge in-place
ft.wait();
std::inplace_merge(begin, mid, end);
}
int main()
{
std::random_device rd;
std::mt19937 rng(rd());
std::uniform_int_distribution<> dist(1,100);
std::vector<int> data;
data.reserve(1024*1024*16);
std::generate_n(std::back_inserter(data), data.capacity(),
[&](){ return dist(rng); });
mergesort(data.begin(), data.end());
std::cout << "threads: " << n_threads << '\n';
}
输出
threads: 1023
您必须相信我,结束 vector 已排序。不会将 16MB 的值转储到此答案中。
注意:这是在 Mac 上使用 clang 3.3 编译和测试的,运行没有问题。不幸的是,我的 gcc 4.7.2 是脑死亡的,因为它在共享计数中止时抛出 cookie,但我对它所在的 libstdc++ 或 VM 没有很高的信心。
关于使用 std::async 时的 C++ "Could not deduce template argument",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22066542/
[我已经编辑了这个问题,因为我简化了导致问题的示例] 我在使用 clang 3.9 的 Travis CI(dist:trusty)上收到以下错误: error: call to implicitly
我是 Haskell 的初学者,正在实现一些基本的代数,比如 class (Eq g) => AbelianGroup g where gplus :: g -> g -> g gne
来自这个问题: Using enum values in combination with SFINAE 我尝试实现: enum Specifier { One, Two, T
If P is a class and P has the form simple-template-id, then the transformed A can be a derived class
这是初学者的问题,但我无法在任何地方识别出任何答案。 以下代码: class A a where foo :: a class A a => B a where bar :: a bar
我正在写一个 Haskell 库,它使用 Data.Vector的。库函数写成功了,不知道怎么加签名。下面是一个说明问题的简单示例: 将合格的 Data.Vector.Generic 导入为 V --
我应该如何在 Deducer 的线性回归模型生成器中生成如下公式 lm(ozone~temp*wind*rad+I(rad^2)+I(temp^2)+I(wind^2)) 在 Outcomes文本框我
temp.names#6 A template-id is valid if there are at most as many arguments as there are parameters o
我有以下代码: class Coll c e where map :: (e1 -> e2) -> c e1 -> c e2 merge :: (e -> e -> e) -> e -
我偶然发现,为什么模板参数演绎在这里不起作用?最近,答案可以归结为“这是一个非演绎的背景”。。具体地说,第一个说是这样的事情,然后重定向到“细节”的标准,而第二个引用的标准,至少可以说是神秘的。。有人
我正在尝试创建另一个 Random 实例,但遇到了类型错误。我将其简化为以下 ghci session : GHCi, version 8.6.5: λ> import System.Random λ
我正在修改以下代码作为作业的一部分: rand :: Random a => State StdGen a rand = do gen (a, a) -> State StdGen a。我编
当我尝试编译这个时: module Main where import qualified Data.Vector.Unboxed.Mutable as MV import Control.Monad
我正试图完成我的一个学校项目,但我遇到了一个问题。我正在尝试在我的工作中使用模板,但似乎我并不真正了解该怎么做。这是我的代码的一部分: 主要.cpp #include "stdafx.h" #incl
这个问题不太可能帮助任何 future 的访问者;它只与一个小的地理区域、一个特定的时间点或一个非常狭窄的情况有关,这些情况并不普遍适用于互联网的全局受众。为了帮助使这个问题更广泛地适用,visit
我正在做一些看起来像这样的包装器: #include template void Apply(void (T::*cb)(Value), T* obj, Value v) { (obj->*
我对 Haskell 的类型系统有以下问题:我试图声明一个数据类型并从函数返回一个包含该类型元素的列表。不幸的是,即使是最小的测试用例,例如 data SampleType = SampleTypeC
来 self 的 previous question ,我一直在尝试制定一些单子(monad)代码。首先,这是我正在使用的状态机函数: import Control.Monad import Cont
我在 .hs 文件中有以下代码 module TypeInference1 where f :: Num a => a -> a -> a f x y = x + y + 3 然后,如果我检查 的类型
这是一个非常简单的程序,我不知道我做错了什么。我在网上看过,但找不到任何有用的东西。我的 getline(cin, movieName) 有问题,但我不知道是什么。 //This program wi
我是一名优秀的程序员,十分优秀!