Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 7 additions & 9 deletions multithreading/message-passing.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,16 @@ has been sent to the thread's mailbox.
## {SourceCode}

```d
import std.stdio : writeln;
import std.concurrency : receive, receiveOnly,
send, spawn, thisTid, Tid;
import std.stdio: writeln;
import std.concurrency;

/*
A custom struct that is used as a message
for a little thread army.
*/
struct NumberMessage {
int number;
this(int i) {
ulong number;
this(ulong i) {
this.number = i;
}
}
Expand Down Expand Up @@ -124,11 +123,10 @@ void main()

// Odd threads get a number, even threads
// a string!
foreach(idx, ref tid; threads) {
import std.string : format;
foreach(size_t idx, ref tid; threads) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure specifying the type here is needed if we use size_t for NumberMessage.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was being explicit, but agreed the type is not needed.

import std.string: format;
if (idx % 2)
send(tid,
NumberMessage(cast(int) idx));
send(tid, NumberMessage(idx));
else
send(tid, format("T=%d", idx));
}
Expand Down