SERVING THE QUANTITATIVE FINANCE COMMUNITY

Cuchulainn
Topic Author
Posts: 61563
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

### C++ 11 Concurrency

C++11 does not have concurrent containers (which is weird IMO) (e.g. Queue) but Microsoft's PPL does supportOk, cest la vie. What to do;1. Use PPL and not C++11 concurrency2. Use C++11 for thread/tasks and PPL for data structures3. Write thread-safe wrappers for STL data containers.?
Last edited by Cuchulainn on August 14th, 2015, 10:00 pm, edited 1 time in total.
http://www.datasimfinancial.com
http://www.datasim.nl

Every Time We Teach a Child Something, We Keep Him from Inventing It Himself
Jean Piaget

Hansi
Posts: 3300
Joined: January 25th, 2010, 11:47 am

### C++ 11 Concurrency

Yes it's by design: http://blogs.msdn.com/b/vcblog/archive/ ... 42.aspxUse a library PPL, Intel TBB or google-concurrency-library

Cuchulainn
Topic Author
Posts: 61563
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

### C++ 11 Concurrency

QuoteOriginally posted by: HansiYes it's by design: http://blogs.msdn.com/b/vcblog/archive/ ... 42.aspxUse a library PPL, Intel TBB or google-concurrency-libraryTheir rationale seems to beQuoteTaking Stephan?s words here, the reality is that they aren?t, not as a bug but as a feature: having every member function of every STL container acquiring an internal lock would annihilate performance. As a general purpose, highly reusable library, it wouldn?t actually provide correctness either: the correct level to place locks is determined by what the program is doing. In that sense, individual member functions don?t tend to be such correct level. Sure. If that is so, then I agree. But who's asking them? why not have 2 versions?BTW I see C++11 does not support thread_group nor interruption_point() (which Boost does).
Last edited by Cuchulainn on August 14th, 2015, 10:00 pm, edited 1 time in total.
http://www.datasimfinancial.com
http://www.datasim.nl

Every Time We Teach a Child Something, We Keep Him from Inventing It Himself
Jean Piaget

Polter
Posts: 2526
Joined: April 29th, 2008, 4:55 pm

### C++ 11 Concurrency

Last edited by Polter on August 14th, 2015, 10:00 pm, edited 1 time in total.

Cuchulainn
Topic Author
Posts: 61563
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

### C++ 11 Concurrency

QuoteFirst, it's indeed highly recommended to use an existing, mature, battle-tested library solution (quality matters -- the flip-side being: there are only so many high-quality choices available, including folks / organizations willing to spend their time / resources to write and defend proposals in this area...):Which ones? C++ concurrency is missing functionality that is standard e.g. reduction vars, loop-level parallelization, producer-consumer.// valarray is a behemoth and is mathematically not even wrong. Transcendental functions of valarrays is mathematical nonsense.
Last edited by Cuchulainn on August 15th, 2015, 10:00 pm, edited 1 time in total.
http://www.datasimfinancial.com
http://www.datasim.nl

Every Time We Teach a Child Something, We Keep Him from Inventing It Himself
Jean Piaget

Cuchulainn
Topic Author
Posts: 61563
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

### C++ 11 Concurrency

QuoteOriginally posted by: PolterRegarding parallelism: It seems that parallel algorithms are coming first:http://www.open-std.org/JTC1/SC22/WG21/ ... 7.pdfSeems reasonable. if you know the interfaces of let's say queue (deque(), enqueue()) then you have to define an extra Bridge to the hardware?queue<T, Impl = std::queue> (using template template param).I suppose getting this into the language is more difficult.Anyways, this seems to be the way C# evolved. (See Joe Duffy's book on C# "Concurrent Programming on Windows".) PPL queue
Last edited by Cuchulainn on August 15th, 2015, 10:00 pm, edited 1 time in total.
http://www.datasimfinancial.com
http://www.datasim.nl

Every Time We Teach a Child Something, We Keep Him from Inventing It Himself
Jean Piaget

Cuchulainn
Topic Author
Posts: 61563
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

### C++ 11 Concurrency

QuoteOriginally posted by: CuchulainnQuoteOriginally posted by: PolterRegarding parallelism: It seems that parallel algorithms are coming first:http://www.open-std.org/JTC1/SC22/WG21/ ... 7.pdfSeems reasonable. if you know the interfaces of let's say queue (deque(), enqueue()) then you have to define an extra Bridge to the hardware?queue<T, Impl = std::queue> (using template template param).I suppose getting this into the language is more difficult.Anyways, this seems to be the way C# evolved. (See Joe Duffy's book on C# "Concurrent Programming on Windows".) PPL queueDoug Schmidt has solved these issues by 1999 in a bunch of articles/books/ACE code by 1999e.g.Strategized Locking, Thread-safe Interface, and Scoped LockingPatterns and Idioms for Simplifying Multi-threaded C++ ComponentsI would expect C++ Concurrency will have something similar when the time comes.BTW The terms 'future' and 'promise' in the "C++ Concurrency in Action" book have been introduced without historical reference but they are in fact at least 30 years old.I think there is lots to do..
Last edited by Cuchulainn on August 17th, 2015, 10:00 pm, edited 1 time in total.
http://www.datasimfinancial.com
http://www.datasim.nl

Every Time We Teach a Child Something, We Keep Him from Inventing It Himself
Jean Piaget

Cuchulainn
Topic Author
Posts: 61563
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

### C++ 11 Concurrency

This code compiles and runs under VS2015 but the values of p1 and p2 are strange after the exchange.. ?? It is either a bug _or_ atomic_exchange is being called wrong.. (curious syntax, BTW) ...(&p1, p2)...
Last edited by Cuchulainn on August 19th, 2015, 10:00 pm, edited 1 time in total.
http://www.datasimfinancial.com
http://www.datasim.nl

Every Time We Teach a Child Something, We Keep Him from Inventing It Himself
Jean Piaget

Polter
Posts: 2526
Joined: April 29th, 2008, 4:55 pm

### C++ 11 Concurrency

std::atomic_exchange replaces (overwrites) the first operand (passed-by-pointer) with the second operand (passed-by-value). If you'd like to access the former value use the return value:http://en.cppreference.com/w/cpp/atomic ... ptr/atomic

Cuchulainn
Topic Author
Posts: 61563
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

### C++ 11 Concurrency

Got it! thanks.The docs say '...(&sp,sp2) Exchange values of sp and sp2". A bit confusing.
http://www.datasimfinancial.com
http://www.datasim.nl

Every Time We Teach a Child Something, We Keep Him from Inventing It Himself
Jean Piaget

Polter
Posts: 2526
Joined: April 29th, 2008, 4:55 pm

### C++ 11 Concurrency

Yeah, it is a bit tricky. Especially since the standard says the following:template<class T>shared_ptr<T> atomic_exchange_explicit(shared_ptr<T>* p, shared_ptr<T> r, memory_order mo);Requires: p shall not be null.Effects: p->swap(r).Returns: The previous value of *p.Throws: Nothing.The "tricky" part is that, even though swap is indeed the specified effect, the second operand r is taken by value (as in a copy, possibly optimized).So, one can conceptually think of this in terms of an atomic swap being performed between the first operand -- and a _temporary copy_ of the second operand (which then gets discarded).Well, atomics are fun, especially with relaxed memory order ;>
Last edited by Polter on August 19th, 2015, 10:00 pm, edited 1 time in total.

Cuchulainn
Topic Author
Posts: 61563
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

### C++ 11 Concurrency

I guess std::atomic<> is a welcome addition. For example, std::atomic<bool> can be used as an alternative to volatile (or non-thread-safe non-volatile types) for thread notification but much of atomics is getting close to the hardware wire. BTW,Are there any developments on libraries/mechanisms atop of C++ Concurrency? For example, taking a 101 example to create a parallel std::accumulate(). This is basically a thread-safe reduction variable like in OpenMP or MPI. It is possible to create a home-grown solution but it is nicer if it is in the language or in a (Boost) library.Of course, writing all these patterns yourself is great fun and/or then move to a library that someone else supports. Another question:How can one use FP-style parallel programming in C++11? Any links?
Last edited by Cuchulainn on August 20th, 2015, 10:00 pm, edited 1 time in total.
http://www.datasimfinancial.com
http://www.datasim.nl

Every Time We Teach a Child Something, We Keep Him from Inventing It Himself
Jean Piaget

dd3
Posts: 246
Joined: June 8th, 2010, 9:02 am

### C++ 11 Concurrency

QuoteOriginally posted by: outrunBoost ASIO which might end up in C++17 has heavy lock congestionsOnly if you use OpenSSL with it