在一次调用中启动多个线程C++;11 < >我在C++中创建多线程的典型方法是: int num_threads = 10; std::thread threads[num_threads]; for(int i = 0; i < num_threads; ++i) { threads[i] = std::thread(doSomething); } // Call join if you need all threads completion for(int i = 0; i < num_threads; ++i) { threads[i].join(); } int num_threads=10; std::thread threads[num_threads]; 对于(int i=0;i线程(逻辑上)。 template<class F> std::future<void> launch_tasks( F&& f, size_t n ) { if (n==0) { // ready future case, launch 0 threads: std::promise<void> p; p.set_value(); return p.get_future(); } std::vector<std::future<void>> results; results.reserve(n-1); for (size_t i = 0; i < n-1; ++i) { results.push_back( std::async( std::launch::async, f, i ) ); } // last thread waits on the previous threads before finishing: return std::async( std::launch::async, [results=std::move(results),f=std::forward<F>(f)]{ f(results.size()); for (auto&& others:results) others.wait(); } }; }
是否可以一次性启动线程,而不是使用循环顺序启动每个线程。我知道在CUDA中,线程是同时启动的,不需要单独启动每个线程。怀疑C++中是否有类似的东西。 < P>是的,你可以生成一个操作,它将在一个语句中启动<代码> N< /Cord>线程(逻辑上)。在一次调用中启动多个线程C++;11 < >我在C++中创建多线程的典型方法是: int num_threads = 10; std::thread threads[num_threads]; for(int i = 0; i < num_threads; ++i) { threads[i] = std::thread(doSomething); } // Call join if you need all threads completion for(int i = 0; i < num_threads; ++i) { threads[i].join(); } int num_threads=10; std::thread threads[num_threads]; 对于(int i=0;i线程(逻辑上)。 template<class F> std::future<void> launch_tasks( F&& f, size_t n ) { if (n==0) { // ready future case, launch 0 threads: std::promise<void> p; p.set_value(); return p.get_future(); } std::vector<std::future<void>> results; results.reserve(n-1); for (size_t i = 0; i < n-1; ++i) { results.push_back( std::async( std::launch::async, f, i ) ); } // last thread waits on the previous threads before finishing: return std::async( std::launch::async, [results=std::move(results),f=std::forward<F>(f)]{ f(results.size()); for (auto&& others:results) others.wait(); } }; },c++,multithreading,c++11,C++,Multithreading,C++11,是否可以一次性启动线程,而不是使用循环顺序启动每个线程。我知道在CUDA中,线程是同时启动的,不需要单独启动每个线程。怀疑C++中是否有类似的东西。 < P>是的,你可以生成一个操作,它将在一个语句中启动 N< /Cord>线程(逻辑上)。 template<class F> std::future<void> launch_tasks( F&& f, size_t n ) { if (n==0) { // ready future case, lau
template<class F>
std::future<void> launch_tasks( F&& f, size_t n ) {
if (n==0) { // ready future case, launch 0 threads:
std::promise<void> p;
p.set_value();
return p.get_future();
}
std::vector<std::future<void>> results;
results.reserve(n-1);
for (size_t i = 0; i < n-1; ++i) {
results.push_back(
std::async(
std::launch::async,
f, i
)
);
}
// last thread waits on the previous threads before finishing:
return std::async(
std::launch::async,
[results=std::move(results),f=std::forward<F>(f)]{
f(results.size());
for (auto&& others:results)
others.wait();
}
};
}
然后
返回工作\u然后\u等待{
标准:正向(f),
标准::移动(结果)
};
而不是lambda,它是等效的,但是用C++11编写的
更简单的版本使用
std::async(std::launch::deferred
处理一个等待所有未来的任务,但这会使等待
和返回的未来的其他定时等待变得无用。我怀疑,对于这些需要,您最好看看MPI:执行线程一次只能做一件事,除非每个线程启动自己和其他线程。Cou请列出c++11解决方案,因为我不知道如何让它工作(因为返回工作\u then\u wait(似乎)与启动任务的返回类型不匹配…这可能是由于我缺乏理解,但这就是为什么我想让它(此解决方案)首先工作的原因…提前谢谢。
template<class F>
struct work_then_wait {
F f;
std::vector<std::future<void>> futures;
void operator()()const{
f(futures.size());
for (auto&& f:results)
f.wait();
}
};
return work_then_wait<typename std::decay<F>::type>{
std::forward<F>(f),
std::move(results)
};