• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

PredatorCZ / PreCore / 536

22 Sep 2024 12:23PM UTC coverage: 52.238% (-3.0%) from 55.265%
536

push

github

PredatorCZ
remove old xml serializers

4084 of 7818 relevant lines covered (52.24%)

10609.73 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

90.2
/include/spike/util/multi_thread.hpp
1
/*  Multi Threading manager
2

3
    Copyright 2019-2022 Lukas Cone
4

5
    Licensed under the Apache License, Version 2.0 (the "License");
6
    you may not use this file except in compliance with the License.
7
    You may obtain a copy of the License at
8

9
      http://www.apache.org/licenses/LICENSE-2.0
10

11
    Unless required by applicable law or agreed to in writing, software
12
    distributed under the License is distributed on an "AS IS" BASIS,
13
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
    See the License for the specific language governing permissions and
15
    limitations under the License.
16
*/
17

18
#pragma once
19
#include <algorithm>
20
#include <chrono>
21
#include <future>
22
#include <thread>
23
#include <vector>
24

25
// lmBody(size_t index)
26
template <class lmBody> void RunThreadedQueue(size_t numTasks, lmBody &&fc) {
2✔
27
  const size_t numHWThreads = std::thread::hardware_concurrency();
2✔
28
  const size_t numThreads = std::min(numHWThreads, numTasks);
2✔
29
  size_t curTask = 0;
2✔
30
  using future_type = std::future<decltype(fc(size_t()))>;
31
  std::vector<future_type> workingThreads(numThreads);
2✔
32

33
  for (auto &wt : workingThreads) {
9✔
34
    wt = std::async(std::launch::async, fc, curTask);
7✔
35
    curTask++;
7✔
36
  }
37

38
  while (curTask < numTasks) {
94✔
39
    for (auto &wt : workingThreads) {
459✔
40
      if (curTask >= numTasks) {
368✔
41
        break;
42
      }
43

44
      if ((wt.wait_for(std::chrono::milliseconds(2)) ==
367✔
45
           std::future_status::ready)) {
46
        wt = std::async(std::launch::async, fc, curTask);
26✔
47
        curTask++;
26✔
48
      }
49
    }
50
  }
51

52
  for (auto &wt : workingThreads) {
9✔
53
    wt.wait();
7✔
54
  }
55
}
2✔
56

1✔
57
// lmBody(size_t index)
1✔
58
template <class lmBody> void RunThreadedQueueEx(size_t numTasks, lmBody &&fc) {
1✔
59
  const size_t numHWThreads = std::thread::hardware_concurrency();
1✔
60
  const size_t numThreads = std::min(numHWThreads, numTasks);
61
  size_t curTask = 0;
1✔
62
  std::vector<std::pair<std::future<void>, std::thread>> threads(numThreads);
63

5✔
64
  auto Invoke = [&](size_t index, std::promise<void> &&state) {
4✔
65
    try {
4✔
66
      fc(index);
67
    } catch (...) {
68
      state.set_exception(std::current_exception());
93✔
69
    }
459✔
70
    state.set_value();
368✔
71
  };
72

73
  auto NewTask = [&](auto &data) {
74
    auto &[future, thread] = data;
367✔
75
    std::promise<void> state;
76

26✔
77
    if (future.valid()) {
26✔
78
      future.get();
79
    }
80

81
    if (thread.joinable()) {
82
      thread.join();
5✔
83
    }
4✔
84

85
    future = state.get_future();
1✔
86
    thread = std::thread(Invoke, curTask, std::move(state));
1✔
87

1✔
88
    curTask++;
1✔
89
  };
1✔
90

91
  for (auto &thread : threads) {
1✔
92
    NewTask(thread);
93
  }
4✔
94

3✔
95
  while (curTask < numTasks) {
3✔
96
    for (auto &thread : threads) {
97
      auto &[future, _] = thread;
98
      if (curTask >= numTasks) {
1✔
99
        break;
×
100
      }
×
101

102
      if ((future.wait_for(std::chrono::milliseconds(2)) ==
103
           std::future_status::ready)) {
104
        NewTask(thread);
×
105
      }
106
    }
×
107
  }
×
108

109
  for (auto &[future, thread] : threads) {
110
    if (future.valid()) {
111
      future.get();
112
    }
4✔
113

3✔
114
    if (thread.joinable()) {
115
      thread.join();
1✔
116
    }
117
  }
118
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc