I am trying to get into programming with D and I have come across something of a problem. I started off using the core.thread Thread class which provides support for starting a thread and then joining the current thread to it. D seems to want people to use message passing instead of lock/synchronization methods so I thought I'd give it a try, but every example of message passing I see needs the tid which I can't seem to get from the Thread class. The code examples I see online actually use spawn which returns the tid instead of using the Thread wrapper, and then you use the tid to pass messages to the Thread. But now there doesn't seem to be any way to join to a thread based on its tid! And not only that but you don't seem to be able to spawn off a delegate, which requires me to add an unnecessary level of indirection.
So my question is first of all, why are there two completely different flavors of threading? And second of all, why are they both so incomplete when together they provide basically everything you could need?
core.thread
provides the basic, low-level primitives for threading. std.concurrency
uses core.thread
internally. A tid
can only be gotten from std.concurrency.spawn
.
So my question is first of all, why are there two completely different flavors of threading?
You might as well ask, why are there two ways to write code, normal D and inline assembly code. There's high(er) level and low level.
And second of all, why are they both so incomplete when together they provide basically everything you could need?
They're not incomplete, but your multithreading must be designed to work in one or the other. If std.concurrency
allowed arbitrary access to Thread
s and such, the guarantees it makes might not be so strong.
To answer your more specific questions, it takes a function
and not a delegate
because a delegate
takes a context pointer, which allows for mutation, which would break some of std.concurrency
's assumptions. Note that spawn
doesn't allow arguments that have mutable indirection, so the no delegates thing shouldn't be shocking.
And instead of joining, you'd send a "stop" message to your thread. Again, this is higher level, and uses higher level constructs.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With