载入中...
搜索中...
未找到
AnimParallelInternal.h
浏览该文件的文档.
1#pragma once
2
3#include <algorithm>
4#include <atomic>
5#include <exception>
6#include <thread>
7#include <vector>
8
10// Synchronous, bounded fork/join for disjoint native animation work. Nothing
11// survives the call, including when thread creation or an item throws.
12template <class Body>
14int parallelAnimationItems(std::size_t count, int requested, Body&& body) {
15 if (count == 0) return 0;
16 const auto limit = requested > 0 ? static_cast<unsigned>(requested) : 8u;
17 const auto workers = std::min({count, static_cast<std::size_t>(limit),
18 static_cast<std::size_t>(std::max(1u, std::thread::hardware_concurrency()))});
20 std::vector<std::exception_ptr> errors(count);
21 std::atomic<std::size_t> next{0};
22 auto run = [&] {
23 for (;;) {
24 const auto index = next.fetch_add(1, std::memory_order_relaxed);
25 if (index >= count) return;
26 try {
28 body(index);
29 } catch (...) {
30 errors[index] = std::current_exception();
31 }
32 }
33 };
34 std::vector<std::thread> threads;
35 threads.reserve(workers - 1);
36 try {
37 for (std::size_t i = 1; i < workers; ++i) threads.emplace_back(run);
38 } catch (...) {
39 for (auto& thread : threads) thread.join();
40 throw;
41 }
43 run();
44 for (auto& thread : threads) thread.join();
45 // Report the first failing input, independent of completion order.
46 for (const auto& error : errors)
47 if (error) std::rethrow_exception(error);
48 return static_cast<int>(workers);
49}
50} // namespace eve::animation::detail
std::weak_ptr< Run > run
Definition OnnxGpgpu.cpp:25
std::string error
Definition Package.cpp:60
std::uint32_t count
int limit
Definition TreeMesh.cpp:164
std::string body
uint32_t index
int parallelAnimationItems(std::size_t count, int requested, Body &&body)
Parallel animation items.