the continuing future of c++ concurrency...supported algorithms the vast majority of the c++...
TRANSCRIPT
![Page 1: The Continuing Future of C++ Concurrency...Supported algorithms The vast majority of the C++ standard algorithms are parallelized, and a few more besides: adjacent_difference adjacent_find](https://reader033.vdocuments.us/reader033/viewer/2022042004/5e6f281786bcb26f4526b947/html5/thumbnails/1.jpg)
The Continuing Future of C++ Concurrency
Anthony Williams
Just Software Solutions Ltdhttp://www.justsoftwaresolutions.co.uk
20th September 2016
![Page 2: The Continuing Future of C++ Concurrency...Supported algorithms The vast majority of the C++ standard algorithms are parallelized, and a few more besides: adjacent_difference adjacent_find](https://reader033.vdocuments.us/reader033/viewer/2022042004/5e6f281786bcb26f4526b947/html5/thumbnails/2.jpg)
The Continuing Future of C++ Concurrency
C++14C++17Technical Specifications:
ConcurrencyParallelismTransactional Memory
![Page 3: The Continuing Future of C++ Concurrency...Supported algorithms The vast majority of the C++ standard algorithms are parallelized, and a few more besides: adjacent_difference adjacent_find](https://reader033.vdocuments.us/reader033/viewer/2022042004/5e6f281786bcb26f4526b947/html5/thumbnails/3.jpg)
Concurrency in C++14
![Page 4: The Continuing Future of C++ Concurrency...Supported algorithms The vast majority of the C++ standard algorithms are parallelized, and a few more besides: adjacent_difference adjacent_find](https://reader033.vdocuments.us/reader033/viewer/2022042004/5e6f281786bcb26f4526b947/html5/thumbnails/4.jpg)
New in C++14
Only one new concurrency feature:std::shared_timed_mutex
std::shared_lock<>
![Page 5: The Continuing Future of C++ Concurrency...Supported algorithms The vast majority of the C++ standard algorithms are parallelized, and a few more besides: adjacent_difference adjacent_find](https://reader033.vdocuments.us/reader033/viewer/2022042004/5e6f281786bcb26f4526b947/html5/thumbnails/5.jpg)
C++14: std::shared_timed_mutex
Multiple threads may hold a shared lock
OR
One thread may hold an exclusive lock
![Page 6: The Continuing Future of C++ Concurrency...Supported algorithms The vast majority of the C++ standard algorithms are parallelized, and a few more besides: adjacent_difference adjacent_find](https://reader033.vdocuments.us/reader033/viewer/2022042004/5e6f281786bcb26f4526b947/html5/thumbnails/6.jpg)
std::shared_timed_mutex: shared locks
std::map<std::string,std::string> table;std::shared_timed_mutex m;std::string find_entry(std::string s){std::shared_lock<
std::shared_timed_mutex> guard(m);auto it=table.find(s);if(it==table.end())
throw std::runtime_error("Not found");return it->second;
}
![Page 7: The Continuing Future of C++ Concurrency...Supported algorithms The vast majority of the C++ standard algorithms are parallelized, and a few more besides: adjacent_difference adjacent_find](https://reader033.vdocuments.us/reader033/viewer/2022042004/5e6f281786bcb26f4526b947/html5/thumbnails/7.jpg)
std::shared_timed_mutex: exclusive locks
std::map<std::string,std::string> table;std::shared_timed_mutex m;
void add_entry(std::string key,std::string value){std::lock_guard<
std::shared_timed_mutex> guard(m);table.insert(std::make_pair(key,value));
}
![Page 8: The Continuing Future of C++ Concurrency...Supported algorithms The vast majority of the C++ standard algorithms are parallelized, and a few more besides: adjacent_difference adjacent_find](https://reader033.vdocuments.us/reader033/viewer/2022042004/5e6f281786bcb26f4526b947/html5/thumbnails/8.jpg)
The timed part of std::shared_timed_mutex
std::shared_timed_mutex m;void foo(){std::shared_lock<std::shared_timed_mutex> sl(
m,std::chrono::seconds(1));if(!sl.owns_lock())return;
do_foo();}
![Page 9: The Continuing Future of C++ Concurrency...Supported algorithms The vast majority of the C++ standard algorithms are parallelized, and a few more besides: adjacent_difference adjacent_find](https://reader033.vdocuments.us/reader033/viewer/2022042004/5e6f281786bcb26f4526b947/html5/thumbnails/9.jpg)
std::shared_timed_mutex performance
Not always an optimization:profile, profile, profileThe std::shared_timed_mutex itself is a pointof contention
![Page 10: The Continuing Future of C++ Concurrency...Supported algorithms The vast majority of the C++ standard algorithms are parallelized, and a few more besides: adjacent_difference adjacent_find](https://reader033.vdocuments.us/reader033/viewer/2022042004/5e6f281786bcb26f4526b947/html5/thumbnails/10.jpg)
Concurrency in C++17
![Page 11: The Continuing Future of C++ Concurrency...Supported algorithms The vast majority of the C++ standard algorithms are parallelized, and a few more besides: adjacent_difference adjacent_find](https://reader033.vdocuments.us/reader033/viewer/2022042004/5e6f281786bcb26f4526b947/html5/thumbnails/11.jpg)
New in C++17
New concurrency features:std::shared_mutex (non-timed)Variadic std::lock_guard<>Deduced std::lock_guard<> andstd::unique_lock<>
Plus: the Parallelism TS v1 has been merged, so thereare parallel versions of most STL algorithms.
Missing: std::joining_thread — the committeedecided this was not worth including.
![Page 12: The Continuing Future of C++ Concurrency...Supported algorithms The vast majority of the C++ standard algorithms are parallelized, and a few more besides: adjacent_difference adjacent_find](https://reader033.vdocuments.us/reader033/viewer/2022042004/5e6f281786bcb26f4526b947/html5/thumbnails/12.jpg)
std::shared_mutex
std::shared_mutex omits the lock-with-timeoutoperations form std::shared_timed_mutex. It issimpler and faster on some platforms.
![Page 13: The Continuing Future of C++ Concurrency...Supported algorithms The vast majority of the C++ standard algorithms are parallelized, and a few more besides: adjacent_difference adjacent_find](https://reader033.vdocuments.us/reader033/viewer/2022042004/5e6f281786bcb26f4526b947/html5/thumbnails/13.jpg)
Variadic std::lock_guard
In C++11 and C++14, std::lock_guard can only beused with a single mutex.
In C++17, you can use std::lock_guard to lockmultiple mutexes in one go, using the samemechanism as std::lock() to avoid deadlock.
std::lock_guard<std::mutex,std::mutex>guard(m1,m2);
![Page 14: The Continuing Future of C++ Concurrency...Supported algorithms The vast majority of the C++ standard algorithms are parallelized, and a few more besides: adjacent_difference adjacent_find](https://reader033.vdocuments.us/reader033/viewer/2022042004/5e6f281786bcb26f4526b947/html5/thumbnails/14.jpg)
Deduced std::lock_guard
In C++11 and C++14, you have to specify the mutextype for std::lock_guard andstd::unique_lock.
std::lock_guard<std::mutex> guard(m);
In C++17, the template parameter can be deducedautomatically.
std::lock_guard guard(m);
![Page 15: The Continuing Future of C++ Concurrency...Supported algorithms The vast majority of the C++ standard algorithms are parallelized, and a few more besides: adjacent_difference adjacent_find](https://reader033.vdocuments.us/reader033/viewer/2022042004/5e6f281786bcb26f4526b947/html5/thumbnails/15.jpg)
Technical Specification for C++Extensions for Concurrency
![Page 16: The Continuing Future of C++ Concurrency...Supported algorithms The vast majority of the C++ standard algorithms are parallelized, and a few more besides: adjacent_difference adjacent_find](https://reader033.vdocuments.us/reader033/viewer/2022042004/5e6f281786bcb26f4526b947/html5/thumbnails/16.jpg)
Concurrency TS v1
Continuations for futuresWaiting for one or all of a set of futuresLatches and BarriersAtomic Smart Pointers
![Page 17: The Continuing Future of C++ Concurrency...Supported algorithms The vast majority of the C++ standard algorithms are parallelized, and a few more besides: adjacent_difference adjacent_find](https://reader033.vdocuments.us/reader033/viewer/2022042004/5e6f281786bcb26f4526b947/html5/thumbnails/17.jpg)
Concurrency TS v2: Proposals Under Consideration
Executors and SchedulersDistributed CountersConcurrent Unordered ContainersConcurrent QueuesSafe concurrent stream accessResumable functions and coroutinesPipelines
![Page 18: The Continuing Future of C++ Concurrency...Supported algorithms The vast majority of the C++ standard algorithms are parallelized, and a few more besides: adjacent_difference adjacent_find](https://reader033.vdocuments.us/reader033/viewer/2022042004/5e6f281786bcb26f4526b947/html5/thumbnails/18.jpg)
Concurrency TS namespace
The concurrency TS provides functions and classes inthe std::experimental namespace.
In the slides I’ll use stdexp instead, as it’s shorter.
namespace stdexp=std::experimental;
![Page 19: The Continuing Future of C++ Concurrency...Supported algorithms The vast majority of the C++ standard algorithms are parallelized, and a few more besides: adjacent_difference adjacent_find](https://reader033.vdocuments.us/reader033/viewer/2022042004/5e6f281786bcb26f4526b947/html5/thumbnails/19.jpg)
Continuations and stdexp::future
A continuation is a new task to run when a futurebecomes readyContinuations are added with the new thenmember functionContinuation functions must take astdexp::future as the only parameterThe source future is no longer valid()Only one continuation can be added
![Page 20: The Continuing Future of C++ Concurrency...Supported algorithms The vast majority of the C++ standard algorithms are parallelized, and a few more besides: adjacent_difference adjacent_find](https://reader033.vdocuments.us/reader033/viewer/2022042004/5e6f281786bcb26f4526b947/html5/thumbnails/20.jpg)
Continuations and stdexp::future
int find_the_answer();std::string process_result(stdexp::future<int>);
auto f=stdexp::async(find_the_answer);auto f2=f.then(process_result);
![Page 21: The Continuing Future of C++ Concurrency...Supported algorithms The vast majority of the C++ standard algorithms are parallelized, and a few more besides: adjacent_difference adjacent_find](https://reader033.vdocuments.us/reader033/viewer/2022042004/5e6f281786bcb26f4526b947/html5/thumbnails/21.jpg)
Exceptions and continuations
int fail(){throw std::runtime_error("failed");
}void next(stdexp::future<int> f){f.get();
}void foo(){auto f=stdexp::async(fail).then(next);f.get();
}
![Page 22: The Continuing Future of C++ Concurrency...Supported algorithms The vast majority of the C++ standard algorithms are parallelized, and a few more besides: adjacent_difference adjacent_find](https://reader033.vdocuments.us/reader033/viewer/2022042004/5e6f281786bcb26f4526b947/html5/thumbnails/22.jpg)
Using lambdas to wrap plain functions
int find_the_answer();std::string process_result(int);
auto f=stdexp::async(find_the_answer);auto f2=f.then([](stdexp::future<int> f){return process_result(f.get());});
![Page 23: The Continuing Future of C++ Concurrency...Supported algorithms The vast majority of the C++ standard algorithms are parallelized, and a few more besides: adjacent_difference adjacent_find](https://reader033.vdocuments.us/reader033/viewer/2022042004/5e6f281786bcb26f4526b947/html5/thumbnails/23.jpg)
Continuations and stdexp::shared_future
Continuations work withstdexp::shared_future as wellThe continuation function must take astdexp::shared_future
The source future remains valid()Multiple continuations can be added
![Page 24: The Continuing Future of C++ Concurrency...Supported algorithms The vast majority of the C++ standard algorithms are parallelized, and a few more besides: adjacent_difference adjacent_find](https://reader033.vdocuments.us/reader033/viewer/2022042004/5e6f281786bcb26f4526b947/html5/thumbnails/24.jpg)
stdexp::shared_future continuations
int find_the_answer();void next1(stdexp::shared_future<int>);int next2(stdexp::shared_future<int>);
auto fi=stdexp::async(find_the_answer).share();
auto f2=fi.then(next1);auto f2=fi.then(next2);
![Page 25: The Continuing Future of C++ Concurrency...Supported algorithms The vast majority of the C++ standard algorithms are parallelized, and a few more besides: adjacent_difference adjacent_find](https://reader033.vdocuments.us/reader033/viewer/2022042004/5e6f281786bcb26f4526b947/html5/thumbnails/25.jpg)
Waiting for the first future to be ready
when_any waits for the first future in the supplied setto be ready. It has two overloads:
template<typename ... Futures>stdexp::future<stdexp::when_any_result<std::tuple<Futures...>>>when_any(Futures... futures);
template<typename Iterator>stdexp::future<stdexp::when_any_result<std::vector<
std::iterator_traits<Iterator>::value_type>>>
when_any(Iterator begin,Iterator end);
![Page 26: The Continuing Future of C++ Concurrency...Supported algorithms The vast majority of the C++ standard algorithms are parallelized, and a few more besides: adjacent_difference adjacent_find](https://reader033.vdocuments.us/reader033/viewer/2022042004/5e6f281786bcb26f4526b947/html5/thumbnails/26.jpg)
when_any
when_any is ideal for:Waiting for speculative tasksWaiting for first results before doing furtherprocessing
auto f1=stdexp::async(foo);auto f2=stdexp::async(bar);auto f3=when_any(std::move(f1),std::move(f2));
f3.then(baz);
![Page 27: The Continuing Future of C++ Concurrency...Supported algorithms The vast majority of the C++ standard algorithms are parallelized, and a few more besides: adjacent_difference adjacent_find](https://reader033.vdocuments.us/reader033/viewer/2022042004/5e6f281786bcb26f4526b947/html5/thumbnails/27.jpg)
Waiting for all futures to be ready
when_all waits for all futures in the supplied set to beready. It has two overloads:
template<typename ... Futures>stdexp::future<std::tuple<Futures...>>when_all(Futures... futures);
template<typename Iterator>stdexp::future<std::vector<
std::iterator_traits<Iterator>::value_type>>
when_all(Iterator begin,Iterator end);
![Page 28: The Continuing Future of C++ Concurrency...Supported algorithms The vast majority of the C++ standard algorithms are parallelized, and a few more besides: adjacent_difference adjacent_find](https://reader033.vdocuments.us/reader033/viewer/2022042004/5e6f281786bcb26f4526b947/html5/thumbnails/28.jpg)
when_all
when_all is ideal for waiting for all subtasks beforecontinuing. Better than calling wait() on each in turn:
auto f1=stdexp::async(subtask1);auto f2=stdexp::async(subtask2);auto f3=stdexp::async(subtask3);auto results=when_all(std::move(f1),std::move(f2),std::move(f3)).get();
![Page 29: The Continuing Future of C++ Concurrency...Supported algorithms The vast majority of the C++ standard algorithms are parallelized, and a few more besides: adjacent_difference adjacent_find](https://reader033.vdocuments.us/reader033/viewer/2022042004/5e6f281786bcb26f4526b947/html5/thumbnails/29.jpg)
Small improvements
The TS also has a couple of small improvements to thestdexp::future interface:
make_ready_future() — creates astdexp::future that is ready, holding the suppliedvaluemake_exceptional_future() — creates astdexp::future that is ready, holding the suppliedexceptionis_ready() member function — returns whether ornot the future is ready
![Page 30: The Continuing Future of C++ Concurrency...Supported algorithms The vast majority of the C++ standard algorithms are parallelized, and a few more besides: adjacent_difference adjacent_find](https://reader033.vdocuments.us/reader033/viewer/2022042004/5e6f281786bcb26f4526b947/html5/thumbnails/30.jpg)
Latches and Barriers
A Latch is a single-use count-downsynchronization mechanism: once Count threadshave decremented the latch it is permanentlysignalled.A Barrier is a reusable count-down synchronizationmechanism: once Count threads havedecremented the barrier, it is reset.
![Page 31: The Continuing Future of C++ Concurrency...Supported algorithms The vast majority of the C++ standard algorithms are parallelized, and a few more besides: adjacent_difference adjacent_find](https://reader033.vdocuments.us/reader033/viewer/2022042004/5e6f281786bcb26f4526b947/html5/thumbnails/31.jpg)
Atomic Smart Pointers
std::shared_ptr<T> and std::weak_ptr<T>are not bitwise-copyable, so you can’t havestd::atomic<std::shared_ptr<T> > orstd::atomic<std::weak_ptr<T> >.
The TS providesstdexp::atomic_shared_ptr<T> andstdexp::atomic_weak_ptr<T> instead.
![Page 32: The Continuing Future of C++ Concurrency...Supported algorithms The vast majority of the C++ standard algorithms are parallelized, and a few more besides: adjacent_difference adjacent_find](https://reader033.vdocuments.us/reader033/viewer/2022042004/5e6f281786bcb26f4526b947/html5/thumbnails/32.jpg)
Concurrency TS:Proposals Under Consideration
![Page 33: The Continuing Future of C++ Concurrency...Supported algorithms The vast majority of the C++ standard algorithms are parallelized, and a few more besides: adjacent_difference adjacent_find](https://reader033.vdocuments.us/reader033/viewer/2022042004/5e6f281786bcb26f4526b947/html5/thumbnails/33.jpg)
Executors and Schedulers
An executor schedules tasks for executionDifferent executors have different schedulingpropertiese.g Thread Pools, Serial executor
![Page 34: The Continuing Future of C++ Concurrency...Supported algorithms The vast majority of the C++ standard algorithms are parallelized, and a few more besides: adjacent_difference adjacent_find](https://reader033.vdocuments.us/reader033/viewer/2022042004/5e6f281786bcb26f4526b947/html5/thumbnails/34.jpg)
Distributed Counters
Distributed counters improve performance by reducingcontention on a global counter.
Counts can be buffered locally to a function or athreadUpdates of the global count can be via push fromeach thread or pull from the reader
![Page 35: The Continuing Future of C++ Concurrency...Supported algorithms The vast majority of the C++ standard algorithms are parallelized, and a few more besides: adjacent_difference adjacent_find](https://reader033.vdocuments.us/reader033/viewer/2022042004/5e6f281786bcb26f4526b947/html5/thumbnails/35.jpg)
Concurrent Unordered Containers
The current proposal is for aconcurrent_unordered_value_map.
No references can be obtained to the storedelementsMany functions return optional<mapped_type>
As well as simple queries like find there are alsomember functions reduce and for_each
![Page 36: The Continuing Future of C++ Concurrency...Supported algorithms The vast majority of the C++ standard algorithms are parallelized, and a few more besides: adjacent_difference adjacent_find](https://reader033.vdocuments.us/reader033/viewer/2022042004/5e6f281786bcb26f4526b947/html5/thumbnails/36.jpg)
Concurrent Queues
A concurrent queue is a vital means of inter-threadcommunication.
Queues may or may not be lock-freeMay be fixed-size of unlimitedMay be closed to prevent additional elementsbeing pushedYou can obtain a “push handle” or “pop handle” forwriting or readingInput and output iterators are supported
![Page 37: The Continuing Future of C++ Concurrency...Supported algorithms The vast majority of the C++ standard algorithms are parallelized, and a few more besides: adjacent_difference adjacent_find](https://reader033.vdocuments.us/reader033/viewer/2022042004/5e6f281786bcb26f4526b947/html5/thumbnails/37.jpg)
Safe concurrent stream access
The standard streams provide limited thread safety —output may be interleavedvoid thread_1(){std::cout<<10<<20<<30;
}void thread_2(){std::cout<<40<<50<<60;
}
output may be104050206030
![Page 38: The Continuing Future of C++ Concurrency...Supported algorithms The vast majority of the C++ standard algorithms are parallelized, and a few more besides: adjacent_difference adjacent_find](https://reader033.vdocuments.us/reader033/viewer/2022042004/5e6f281786bcb26f4526b947/html5/thumbnails/38.jpg)
Safe concurrent stream access
We need a way to group output from several inserts:basic_ostream_buffer<char>
void thread_1(){basic_ostream_buffer<char> buf(std::cout);
buf<<10<<20<<30;} // buf destroyed// contents written to std::cout
![Page 39: The Continuing Future of C++ Concurrency...Supported algorithms The vast majority of the C++ standard algorithms are parallelized, and a few more besides: adjacent_difference adjacent_find](https://reader033.vdocuments.us/reader033/viewer/2022042004/5e6f281786bcb26f4526b947/html5/thumbnails/39.jpg)
Resumable functions and coroutines
Coroutines expose a “pull” interface for callback-styleimplementations.
Resumable functions automatically generate asynccalls from code that waits on futures.
Both provide alternative ways of structuring code thatdoes asynchronous operations.
![Page 40: The Continuing Future of C++ Concurrency...Supported algorithms The vast majority of the C++ standard algorithms are parallelized, and a few more besides: adjacent_difference adjacent_find](https://reader033.vdocuments.us/reader033/viewer/2022042004/5e6f281786bcb26f4526b947/html5/thumbnails/40.jpg)
Pipelines
The pipeline proposal is a way of wrapping concurrentqueues and tasks:
queue<InputType> source;queue<OutputType> sink;pipeline::from(source) |pipeline::parallel(foo,num_threads) |bar | baz | sink;
![Page 41: The Continuing Future of C++ Concurrency...Supported algorithms The vast majority of the C++ standard algorithms are parallelized, and a few more besides: adjacent_difference adjacent_find](https://reader033.vdocuments.us/reader033/viewer/2022042004/5e6f281786bcb26f4526b947/html5/thumbnails/41.jpg)
Hazard Pointers
Hazard pointers provide an answer to the question:
When is it safe to delete this object?
It’s an alternative to GC or reference counting.
![Page 42: The Continuing Future of C++ Concurrency...Supported algorithms The vast majority of the C++ standard algorithms are parallelized, and a few more besides: adjacent_difference adjacent_find](https://reader033.vdocuments.us/reader033/viewer/2022042004/5e6f281786bcb26f4526b947/html5/thumbnails/42.jpg)
Hazard Pointers
Hazard pointers provide an answer to the question:
When is it safe to delete this object?
It’s an alternative to GC or reference counting.
![Page 43: The Continuing Future of C++ Concurrency...Supported algorithms The vast majority of the C++ standard algorithms are parallelized, and a few more besides: adjacent_difference adjacent_find](https://reader033.vdocuments.us/reader033/viewer/2022042004/5e6f281786bcb26f4526b947/html5/thumbnails/43.jpg)
Hazard Pointers
“Using” thread:
obtains a pointer to an object Xmarks that it is accessing Xwith a hazard pointerchecks it is still safe to use Xuses Xreleases hazard pointer to Xdeletes X if marked and noother hazard pointers to X
“Deleting” thread:
obtains a pointer to an object Xmarks that X is to be deletedchecks hazard pointers for Xif no hazard pointers, deletes X
![Page 44: The Continuing Future of C++ Concurrency...Supported algorithms The vast majority of the C++ standard algorithms are parallelized, and a few more besides: adjacent_difference adjacent_find](https://reader033.vdocuments.us/reader033/viewer/2022042004/5e6f281786bcb26f4526b947/html5/thumbnails/44.jpg)
Further proposals
There are more proposals not covered here.
See the C++ committee websitehttp://www.open-std.org/jtc1/sc22/wg21/
and the ISO C++ Foundation https://isocpp.org.
![Page 45: The Continuing Future of C++ Concurrency...Supported algorithms The vast majority of the C++ standard algorithms are parallelized, and a few more besides: adjacent_difference adjacent_find](https://reader033.vdocuments.us/reader033/viewer/2022042004/5e6f281786bcb26f4526b947/html5/thumbnails/45.jpg)
Technical Specification for C++Extensions for Parallelism
![Page 46: The Continuing Future of C++ Concurrency...Supported algorithms The vast majority of the C++ standard algorithms are parallelized, and a few more besides: adjacent_difference adjacent_find](https://reader033.vdocuments.us/reader033/viewer/2022042004/5e6f281786bcb26f4526b947/html5/thumbnails/46.jpg)
Parallelism TS
Parallelism TS v1 (merged to C++17):Parallel algorithmsMapreduceLightweight Execution AgentsSIMD and Vector algorithms
Parallelism TS v2:Task Blocks
![Page 47: The Continuing Future of C++ Concurrency...Supported algorithms The vast majority of the C++ standard algorithms are parallelized, and a few more besides: adjacent_difference adjacent_find](https://reader033.vdocuments.us/reader033/viewer/2022042004/5e6f281786bcb26f4526b947/html5/thumbnails/47.jpg)
Parallel Algorithms
The v1 TS (and thus C++17) provides a new set ofoverloads of the standard library algorithms with anexecution policy parameter:
template<typename ExecutionPolicy,typename Iterator,typename Function>
void for_each(ExecutionPolicy&& policy,Iterator begin,Iterator end,Function f);
![Page 48: The Continuing Future of C++ Concurrency...Supported algorithms The vast majority of the C++ standard algorithms are parallelized, and a few more besides: adjacent_difference adjacent_find](https://reader033.vdocuments.us/reader033/viewer/2022042004/5e6f281786bcb26f4526b947/html5/thumbnails/48.jpg)
Execution Policies
The execution policy may be:
std::sequential — sequential execution on thecalling threadstd::par — indeterminately sequenced executionon unspecified threadsstd::par_vec — unsequenced execution onunspecified threads
![Page 49: The Continuing Future of C++ Concurrency...Supported algorithms The vast majority of the C++ standard algorithms are parallelized, and a few more besides: adjacent_difference adjacent_find](https://reader033.vdocuments.us/reader033/viewer/2022042004/5e6f281786bcb26f4526b947/html5/thumbnails/49.jpg)
Supported algorithms
The vast majority of the C++ standard algorithms are parallelized,and a few more besides:adjacent_difference adjacent_find all_of any_of copy copy_if copy_ncount count_if equal exclusive_scan fill fill_n find find_endfind_first_of find_if find_if_not for_each for_each_n generategenerate_n includes inclusive_scan inner_product inplace_merge is_heapis_heap_until is_partitioned is_sorted is_sorted_untillexicographical_compare max_element merge min_element minmax_elementmismatch move none_of nth_element partial_sort partial_sort_copypartition partition_copy reduce remove remove_copy remove_copy_ifremove_if replace replace_copy replace_copy_if replace_if reversereverse_copy rotate rotate_copy search search_n set_differenceset_intersection set_symmetric_difference set_union sortstable_partition stable_sort swap_ranges transformtransform_exclusive_scan transform_inclusive_scan transform_reduceuninitialized_copy uninitialized_copy_n uninitialized_filluninitialized_fill_n unique unique_copy
![Page 50: The Continuing Future of C++ Concurrency...Supported algorithms The vast majority of the C++ standard algorithms are parallelized, and a few more besides: adjacent_difference adjacent_find](https://reader033.vdocuments.us/reader033/viewer/2022042004/5e6f281786bcb26f4526b947/html5/thumbnails/50.jpg)
Parallelism TS v2: Task Blocks
Task blocks allow for managing hierarchies of tasks:
Nested task blocks within an outer task block canrun in parallelAll nested task blocks created within a task regionare complete when the region exitsTask blocks can be nested
![Page 51: The Continuing Future of C++ Concurrency...Supported algorithms The vast majority of the C++ standard algorithms are parallelized, and a few more besides: adjacent_difference adjacent_find](https://reader033.vdocuments.us/reader033/viewer/2022042004/5e6f281786bcb26f4526b947/html5/thumbnails/51.jpg)
Transactional Memory for C++
![Page 52: The Continuing Future of C++ Concurrency...Supported algorithms The vast majority of the C++ standard algorithms are parallelized, and a few more besides: adjacent_difference adjacent_find](https://reader033.vdocuments.us/reader033/viewer/2022042004/5e6f281786bcb26f4526b947/html5/thumbnails/52.jpg)
Transactional Memory
Two basic types of “transaction” blocks: synchronizedblocks and atomic blocks
Synchronized blocks introduced with thesynchronized keywordAtomic blocks introduced with one ofatomic_noexcept, atomic_commit oratomic_cancel
![Page 53: The Continuing Future of C++ Concurrency...Supported algorithms The vast majority of the C++ standard algorithms are parallelized, and a few more besides: adjacent_difference adjacent_find](https://reader033.vdocuments.us/reader033/viewer/2022042004/5e6f281786bcb26f4526b947/html5/thumbnails/53.jpg)
Synchronized blocks
Synchronized blocks behave as if they lock a globalmutex.int i;void foo(){synchronized {++i;
}}
![Page 54: The Continuing Future of C++ Concurrency...Supported algorithms The vast majority of the C++ standard algorithms are parallelized, and a few more besides: adjacent_difference adjacent_find](https://reader033.vdocuments.us/reader033/viewer/2022042004/5e6f281786bcb26f4526b947/html5/thumbnails/54.jpg)
Atomic blocks
Atomic execute atomically and not concurrently withany synchronized blocks.int i;void bar(){atomic_noexcept {++i;
}}
![Page 55: The Continuing Future of C++ Concurrency...Supported algorithms The vast majority of the C++ standard algorithms are parallelized, and a few more besides: adjacent_difference adjacent_find](https://reader033.vdocuments.us/reader033/viewer/2022042004/5e6f281786bcb26f4526b947/html5/thumbnails/55.jpg)
Atomic blocks may be concurrent
Atomic may execute concurrently if no conflictsint i,j;void bar(){atomic_noexcept { ++i; }
}void baz(){atomic_noexcept { ++j; }
}
![Page 56: The Continuing Future of C++ Concurrency...Supported algorithms The vast majority of the C++ standard algorithms are parallelized, and a few more besides: adjacent_difference adjacent_find](https://reader033.vdocuments.us/reader033/viewer/2022042004/5e6f281786bcb26f4526b947/html5/thumbnails/56.jpg)
Atomic blocks and exceptions
The atomic blocks differ in their behaviour withexceptions:
atomic_noexcept — escaping exceptions causeundefined behaviouratomic_commit — escaping exceptions committhe transactionatomic_cancel — escaping exceptions roll backthe transaction, but must be transaction safe
![Page 57: The Continuing Future of C++ Concurrency...Supported algorithms The vast majority of the C++ standard algorithms are parallelized, and a few more besides: adjacent_difference adjacent_find](https://reader033.vdocuments.us/reader033/viewer/2022042004/5e6f281786bcb26f4526b947/html5/thumbnails/57.jpg)
Questions?
![Page 58: The Continuing Future of C++ Concurrency...Supported algorithms The vast majority of the C++ standard algorithms are parallelized, and a few more besides: adjacent_difference adjacent_find](https://reader033.vdocuments.us/reader033/viewer/2022042004/5e6f281786bcb26f4526b947/html5/thumbnails/58.jpg)
Just::Thread Pro
just::thread Pro provides an actor framework, a concurrent hashmap, a concurrent queue, synchronized values and a complete
implementation of the C++ Concurrency TS, including a lock-freeimplementation of atomic_shared_ptr.
http://stdthread.co.uk
![Page 59: The Continuing Future of C++ Concurrency...Supported algorithms The vast majority of the C++ standard algorithms are parallelized, and a few more besides: adjacent_difference adjacent_find](https://reader033.vdocuments.us/reader033/viewer/2022042004/5e6f281786bcb26f4526b947/html5/thumbnails/59.jpg)
My Book
C++ Concurrency in Action:Practical Multithreading
http://stdthread.com/book