Skip to content

Commit 816d3f7

Browse files
committed
Fix concurrent class. Only works with gcc 4.7. Broken in clang 3.2 and
gcc 4.8.
1 parent 7aba4cb commit 816d3f7

File tree

2 files changed

+27
-8
lines changed

2 files changed

+27
-8
lines changed

Concurrency/ConcurrentWrapper2/Concurrent.h

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,14 @@ class Concurrent
6868
worker_.join();
6969
}
7070

71+
private:
72+
73+
mutable Queue<std::function<void()>> queue_;
74+
mutable T resource_;
75+
std::thread worker_;
76+
bool done_;
77+
78+
public:
7179
template <typename F>
7280
auto operator()(F f) const -> std::future<decltype(f(resource_))>
7381
{
@@ -81,11 +89,5 @@ class Concurrent
8189
return fut;
8290
}
8391

84-
private:
85-
86-
mutable Queue<std::function<void()>> queue_;
87-
mutable T resource_;
88-
std::thread worker_;
89-
bool done_;
9092

9193
};

Concurrency/ConcurrentWrapper2/concurrent_vector.cpp

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,29 @@ std::ostream& operator<<(std::ostream& os, const std::vector<T>& v)
1515

1616
int main()
1717
{
18-
Concurrent<std::vector<int>> cv{{1, 2, 3, 4, 5, 6}};
19-
18+
Concurrent<std::vector<int>> cv{std::vector<int>(10)};
2019
auto f1 = cv([](std::vector<int>& v){
2120
return std::accumulate(v.begin(), v.end(), 0);
2221
});
22+
23+
auto f2 = cv([](std::vector<int>& v)->bool{
24+
std::iota(v.begin(), v.end(), 0);
25+
return true;
26+
});
27+
auto f3 = cv([](std::vector<int>& v){
28+
return std::accumulate(v.begin(), v.end(), 0);
29+
});
30+
31+
auto f4 = cv([](std::vector<int>& v){
32+
return v.size();
33+
});
34+
35+
std::cout << std::boolalpha;
2336
std::cout << f1.get() << std::endl;
37+
std::cout << f2.get() << std::endl;
38+
std::cout << f3.get() << std::endl;
39+
std::cout << f4.get() << std::endl;
40+
2441

2542
}
2643

0 commit comments

Comments
 (0)