digitalmars.D - Safe & Performant Inter-Thread Message Passing
- =?UTF-8?B?Ik5vcmRsw7Z3Ig==?= (10/10) Feb 18 2014 What's the best solution to communicate between threads in D
- deadalnix (8/18) Feb 18 2014 In its current implementation, it uses mutexes (or did last time
- =?UTF-8?B?Ik5vcmRsw7Z3Ig==?= (13/35) Feb 18 2014 That is good to know.
- =?UTF-8?B?Ik5vcmRsw7Z3Ig==?= (2/3) Feb 18 2014 I of course mean lock-free queues.
- Andrei Alexandrescu (3/6) Feb 18 2014 std.allocator has one.
- Stanislav Blinov (6/14) Feb 18 2014 I'd say "upcoming std.allocator will have one". It's not in
- =?UTF-8?B?Ik5vcmRsw7Z3Ig==?= (5/7) Feb 19 2014 Great to hear!
- John Colvin (2/35) Feb 18 2014 see http://dlang.org/phobos-prerelease/core_atomic.html
- deadalnix (8/47) Feb 18 2014 MessageBox can have several producer but simply one consumer. I
- John Colvin (13/23) Feb 18 2014 Performance and inter-thread communication is heavily dependant
What's the best solution to communicate between threads in D today if I care about 1. Security & Correctness? 2. Performance? and are these mutually exclusive? Does Phobos or other library contain lockfree queues? From what I can see std.concurrent.MessageBox requires Mutex-locking. Is this really needed or can it be enhanced to support lockfree communication similar to what Boost.Lockfree does?
Feb 18 2014
On Tuesday, 18 February 2014 at 21:05:38 UTC, Nordlöw wrote:What's the best solution to communicate between threads in D today if I care about 1. Security & Correctness? 2. Performance? and are these mutually exclusive? Does Phobos or other library contain lockfree queues? From what I can see std.concurrent.MessageBox requires Mutex-locking. Is this really needed or can it be enhanced to support lockfree communication similar to what Boost.Lockfree does?In its current implementation, it uses mutexes (or did last time I checked). However, this isn't required. I wanted to implement something a while ago, but was stopped by the fact that DMD doesn't implement shared as per spec (ensure sequential consistency). It is still possible to do with primitives for concurrency available in the runtime. If you plan to submit a pull request, I'll be happy to review.
Feb 18 2014
On Tuesday, 18 February 2014 at 21:21:54 UTC, deadalnix wrote:On Tuesday, 18 February 2014 at 21:05:38 UTC, Nordlöw wrote:That is good to know. I have no experience in writing threadlocking queues. I'm just aware of what's state of the art. It would be interesting to take look though... :) A few thoughts and questions: - I do know that you construct it using CPU intrinsics like CAS and that single producer single consumer queues is the easiest one to get right. - This should suffice for the needs of MessageBox right? - I guess we need to make copies of all the messages right? ThxWhat's the best solution to communicate between threads in D today if I care about 1. Security & Correctness? 2. Performance? and are these mutually exclusive? Does Phobos or other library contain lockfree queues? From what I can see std.concurrent.MessageBox requires Mutex-locking. Is this really needed or can it be enhanced to support lockfree communication similar to what Boost.Lockfree does?In its current implementation, it uses mutexes (or did last time I checked). However, this isn't required. I wanted to implement something a while ago, but was stopped by the fact that DMD doesn't implement shared as per spec (ensure sequential consistency). It is still possible to do with primitives for concurrency available in the runtime. If you plan to submit a pull request, I'll be happy to review.
Feb 18 2014
I have no experience in writing threadlocking queues.I of course mean lock-free queues. /Per
Feb 18 2014
On 2/19/14, 12:20 AM, "Nordlöw" wrote:std.allocator has one. AndreiI have no experience in writing threadlocking queues.I of course mean lock-free queues. /Per
Feb 18 2014
On Tuesday, 18 February 2014 at 23:45:37 UTC, Andrei Alexandrescu wrote:On 2/19/14, 12:20 AM, "Nordlöw" wrote:I'd say "upcoming std.allocator will have one". It's not in Phobos yet and not everybody knows they need to go to https://github.com/andralex/phobos/blob/allocator/std/allocator.d to get their hands on std.allocator.std.allocator has one. AndreiI have no experience in writing threadlocking queues.I of course mean lock-free queues. /Per
Feb 18 2014
std.allocator has one. AndreiGreat to hear! What's the review status on std.allocator? I believe a lock-free solution would surely outperform a mutex-lock-based solution for many small messages. Have anybody benchmarked the overhead of D's Mutex lock/unlocks?
Feb 19 2014
On Tuesday, 18 February 2014 at 21:37:09 UTC, Nordlöw wrote:On Tuesday, 18 February 2014 at 21:21:54 UTC, deadalnix wrote:see http://dlang.org/phobos-prerelease/core_atomic.htmlOn Tuesday, 18 February 2014 at 21:05:38 UTC, Nordlöw wrote:That is good to know. I have no experience in writing threadlocking queues. I'm just aware of what's state of the art. It would be interesting to take look though... :) A few thoughts and questions: - I do know that you construct it using CPU intrinsics like CASWhat's the best solution to communicate between threads in D today if I care about 1. Security & Correctness? 2. Performance? and are these mutually exclusive? Does Phobos or other library contain lockfree queues? From what I can see std.concurrent.MessageBox requires Mutex-locking. Is this really needed or can it be enhanced to support lockfree communication similar to what Boost.Lockfree does?In its current implementation, it uses mutexes (or did last time I checked). However, this isn't required. I wanted to implement something a while ago, but was stopped by the fact that DMD doesn't implement shared as per spec (ensure sequential consistency). It is still possible to do with primitives for concurrency available in the runtime. If you plan to submit a pull request, I'll be happy to review.
Feb 18 2014
On Tuesday, 18 February 2014 at 21:37:09 UTC, Nordlöw wrote:On Tuesday, 18 February 2014 at 21:21:54 UTC, deadalnix wrote:MessageBox can have several producer but simply one consumer. I had something similar to disruptor in mind : http://www.slideshare.net/trishagee/introduction-to-the-disruptor We don't need to make copies. Actually, std.concurency accept only values types (that will be copied as they are value types), immutable (that do not need copy as they are immutable) and shared (where it is up to the programmer to ensure correctness).On Tuesday, 18 February 2014 at 21:05:38 UTC, Nordlöw wrote:That is good to know. I have no experience in writing threadlocking queues. I'm just aware of what's state of the art. It would be interesting to take look though... :) A few thoughts and questions: - I do know that you construct it using CPU intrinsics like CAS and that single producer single consumer queues is the easiest one to get right. - This should suffice for the needs of MessageBox right? - I guess we need to make copies of all the messages right? ThxWhat's the best solution to communicate between threads in D today if I care about 1. Security & Correctness? 2. Performance? and are these mutually exclusive? Does Phobos or other library contain lockfree queues? From what I can see std.concurrent.MessageBox requires Mutex-locking. Is this really needed or can it be enhanced to support lockfree communication similar to what Boost.Lockfree does?In its current implementation, it uses mutexes (or did last time I checked). However, this isn't required. I wanted to implement something a while ago, but was stopped by the fact that DMD doesn't implement shared as per spec (ensure sequential consistency). It is still possible to do with primitives for concurrency available in the runtime. If you plan to submit a pull request, I'll be happy to review.
Feb 18 2014
On Tuesday, 18 February 2014 at 21:05:38 UTC, Nordlöw wrote:What's the best solution to communicate between threads in D today if I care about 1. Security & Correctness? 2. Performance? and are these mutually exclusive? Does Phobos or other library contain lockfree queues? From what I can see std.concurrent.MessageBox requires Mutex-locking. Is this really needed or can it be enhanced to support lockfree communication similar to what Boost.Lockfree does?Performance and inter-thread communication is heavily dependant on the use-case, including the details of memory access patterns etc. (cache misses can hurt you more than lock overheads ever will) Most benchmarks that show amazing lock-free performance scaling deal with pathological cases. A lot of real-world code can be plenty fast (or even faster) with message-passing or (sometimes even) locks. Security and correctness: message passing wins here, no question. Data is copied between threads, no possibility of races etc. I really like std.concurrency for this: very intuitive, easy to use and pretty fast.
Feb 18 2014