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

Razakhel / RaZ / 8116214568

01 Mar 2024 07:24PM UTC coverage: 79.177% (-0.007%) from 79.184%
8116214568

push

github

Razakhel
[Utils/Threading] Fixed the per-thread range calculation

- The previous calculation skipped the last value in some cases

- Reworked the tests so that several sizes are checked, crossed with different number of threads

- Giving a thread count of 0 to parallelization functions now throws an exception instead of doing an assertion

11 of 15 new or added lines in 2 files covered. (73.33%)

7905 of 9984 relevant lines covered (79.18%)

764.91 hits per line

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

96.67
/src/RaZ/Utils/Threading.cpp
1
#include "RaZ/Utils/Threading.hpp"
2

3
#ifdef RAZ_THREADS_AVAILABLE
4

5
namespace Raz::Threading {
6

7
ThreadPool& getDefaultThreadPool() {
296✔
8
  static ThreadPool threadPool;
296✔
9
  return threadPool;
296✔
10
}
11

12
void parallelize(const std::function<void()>& action, unsigned int threadCount) {
1✔
13
  if (threadCount == 0)
1✔
NEW
14
    throw std::invalid_argument("[Threading] The number of threads cannot be 0.");
×
15

16
#if !defined(RAZ_PLATFORM_EMSCRIPTEN)
17
  ThreadPool& threadPool = getDefaultThreadPool();
1✔
18

19
  std::vector<std::promise<void>> promises;
1✔
20
  promises.resize(threadCount);
1✔
21

22
  for (unsigned int i = 0; i < threadCount; ++i) {
11✔
23
    threadPool.addAction([&action, &promises, i] () {
10✔
24
      action();
10✔
25
      promises[i].set_value();
10✔
26
    });
10✔
27
  }
28

29
  // Blocking here to wait for all threads to finish their action
30
  for (std::promise<void>& promise : promises)
11✔
31
    promise.get_future().wait();
10✔
32
#else
33
  for (unsigned int i = 0; i < threadCount; ++i)
34
    action();
35
#endif
36
}
1✔
37

38
void parallelize(std::initializer_list<std::function<void()>> actions) {
2✔
39
#if !defined(RAZ_PLATFORM_EMSCRIPTEN)
40
  ThreadPool& threadPool = getDefaultThreadPool();
2✔
41

42
  std::vector<std::promise<void>> promises;
2✔
43
  promises.resize(actions.size());
2✔
44

45
  for (unsigned int i = 0; i < actions.size(); ++i) {
7✔
46
    threadPool.addAction([&actions, &promises, i] () {
5✔
47
      const std::function<void()>& action = *(actions.begin() + i);
5✔
48
      action();
5✔
49
      promises[i].set_value();
5✔
50
    });
5✔
51
  }
52

53
  // Blocking here to wait for all threads to finish their action
54
  for (std::promise<void>& promise : promises)
7✔
55
    promise.get_future().wait();
5✔
56
#else
57
  for (const std::function<void()>& action : actions)
58
    action();
59
#endif
60
}
2✔
61

62
} // namespace Raz::Threading
63

64
#endif // RAZ_THREADS_AVAILABLE
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