Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Small change to avoid the implicit cast on foreach #361

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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) {
MikeShah marked this conversation as resolved.
Show resolved Hide resolved
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