|
4 | 4 | /// on C++ concurrency by Herb Sutter: |
5 | 5 | /// http://channel9.msdn.com/Shows/Going+Deep/C-and-Beyond-2012-Herb-Sutter-Concurrency-and-Parallelism |
6 | 6 | /// |
7 | | -/// Tweaked to compile with g++ 4.7.2. |
| 7 | +/// This is an extension of the concurrent wrapper presented in this blog: |
| 8 | +/// http://juanchopanzacpp.wordpress.com/2013/03/01/concurrent-object-wrapper-c11/ |
| 9 | +/// |
| 10 | +/// Currently does not compile on gcc 4.7 or 4.8 due to a compiler bug related |
| 11 | +/// to decltype of member variables used in a trailing return type. |
| 12 | +/// This is expected to be fixed in GCC 4.8.1. |
8 | 13 | /// |
9 | 14 | /// Wraps a shared resource such that callers can request operations on the resource asynchronously. |
10 | 15 | /// Each request is treated as an atomic transaction, and the caller side is not blocked. |
|
15 | 20 | /// Example useage: |
16 | 21 | /// |
17 | 22 | /// @code |
18 | | -/// Concurrent<std::string> cs; |
| 23 | +/// Concurrent<std::vector<int>> cv; |
| 24 | +/// |
| 25 | +/// //1: Resize the vector, fill with sequence |
| 26 | +/// auto f1 = cv([](std::vector<int>& v)->size_t{ v.resize(10); |
| 27 | +/// std::iota(v.begin(), v.end(), 0); |
| 28 | +/// return v.size(); |
| 29 | +/// }); |
19 | 30 | /// |
20 | | -/// //Thread 1: just print the string |
21 | | -/// cs([](const std::string& s){ std::cout << s;}); |
| 31 | +/// //2. Calculate the sum of elements |
| 32 | +/// auto f2 = cv([](const std::vector<int>& v){ return std::accumulate(v.begin(), v.end(), 0);}); |
22 | 33 | /// |
23 | | -/// // Thread 2: |
24 | | -/// void foo(std::string& s); |
25 | | -/// void bar(std::string& s); |
| 34 | +/// //3: Call some user defined functions |
| 35 | +/// int foo(std::vector<int>& v); |
| 36 | +/// void bar(std::vector<int>& v); |
26 | 37 | /// // run bar, then foo on the shared resource |
27 | | -/// cs([] (std::string& s) { bar(s); foo(s);}); |
28 | | -/// |
29 | | -/// // Thread 3: make string uppercase |
30 | | -/// cs([](std::string& s){ for (auto& c : s) c = static_cast<char>(std::toupper(c));)} ); |
| 38 | +/// auto f3 = cs([] (std::vector<int>& v)->int { bar(v); |
| 39 | +/// return foo(v); |
| 40 | +/// }); |
31 | 41 | /// |
| 42 | +/// //4 get the results when needed |
| 43 | +/// std::cout << f1.get() << " " << f2.get() " " << f3.get() << std::endl; |
32 | 44 | /// |
33 | 45 |
|
34 | 46 | #include <functional> |
|
0 commit comments