TARAXA
Loading...
Searching...
No Matches
rpc_utils.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <json/json.h>
4
5#include <atomic>
6#include <thread>
7
8#include "thread_pool.hpp"
9
10namespace taraxa::util {
11
12template <class S, class FN>
13Json::Value transformToJsonParallel(const S& source, FN op) {
14 if (source.empty()) {
15 return Json::Value(Json::arrayValue);
16 }
17 static util::ThreadPool executor{std::thread::hardware_concurrency() / 2};
18
19 Json::Value out(Json::arrayValue);
20 out.resize(source.size());
21 std::atomic_uint processed = 0;
22 for (unsigned i = 0; i < source.size(); ++i) {
23 executor.post([&, i]() {
24 out[i] = op(source[i], i);
25 ++processed;
26 });
27 }
28
29 while (true) {
30 if (processed == source.size()) {
31 break;
32 }
33 }
34 return out;
35}
36
37Json::Value mergeJsons(Json::Value&& o1, Json::Value&& o2);
38
39} // namespace taraxa::util
Definition thread_pool.hpp:7
Definition default_construct_copyable_movable.hpp:10
Json::Value transformToJsonParallel(const S &source, FN op)
Definition rpc_utils.hpp:13
Json::Value mergeJsons(Json::Value &&o1, Json::Value &&o2)
Definition rpc_utils.cpp:5