File tree 1 file changed +24
-13
lines changed
1 file changed +24
-13
lines changed Original file line number Diff line number Diff line change 19
19
#include < thread>
20
20
#include < vector>
21
21
22
+ #include < algorithm>
23
+
22
24
namespace clang {
23
25
namespace mrdox {
24
26
25
27
/* * Visit all elements of a range concurrently.
26
28
*/
27
29
template <
28
- class Range ,
29
- class Worker ,
30
+ class Elements ,
31
+ class Workers ,
30
32
class ... Args>
31
33
Error
32
34
parallelFor (
33
- Range&& range ,
34
- std::vector<Worker> & workers,
35
+ Elements& elements ,
36
+ Workers & workers,
35
37
Args&&... args)
36
38
{
39
+ if (std::next (std::begin (workers)) == std::end (workers))
40
+ {
41
+ // Non-concurrent
42
+ auto && worker (*std::begin (workers));
43
+ for (auto && element : elements)
44
+ if (! worker (element, std::forward<Args>(args)...))
45
+ return Error (" canceled" );
46
+ return Error::success ();
47
+ }
48
+
37
49
std::mutex m;
38
50
bool cancel = false ;
39
- auto it = std::begin (range );
40
- auto const end = std::end (range );
51
+ auto it = std::begin (elements );
52
+ auto const end = std::end (elements );
41
53
auto const do_work =
42
- [&](Worker& worker )
54
+ [&](auto && agent )
43
55
{
44
56
std::unique_lock<std::mutex> lock (m);
45
57
if (it == end || cancel)
46
58
return false ;
47
59
auto it0 = it;
48
60
++it;
49
61
lock.unlock ();
50
- bool cancel_ = worker (*it0,
62
+ bool cancel_ = ! agent (*it0,
51
63
std::forward<Args>(args)...);
52
64
if (! cancel_)
53
65
return true ;
54
66
cancel = true ;
55
67
return false ;
56
68
};
57
69
std::vector<std::thread> threads;
58
- threads.reserve (workers.size ());
59
- for (std::size_t i = 0 ; i < workers.size (); ++i)
70
+ for (auto & worker : workers)
60
71
threads.emplace_back (std::thread (
61
- [&](std:: size_t i_ )
72
+ [&](auto && agent )
62
73
{
63
74
for (;;)
64
- if (! do_work (workers[i] ))
75
+ if (! do_work (agent ))
65
76
break ;
66
- }, i ));
77
+ }, worker ));
67
78
for (auto & t : threads)
68
79
t.join ();
69
80
if (cancel)
You can’t perform that action at this time.
0 commit comments