Skip to content

TRPL: dining philosophers #25321

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

Merged
merged 1 commit into from
May 13, 2015
Merged

Conversation

steveklabnik
Copy link
Member

This is a little rough, and it needs squashed and section headers, but i'd like to get some eyes on it sooner rather than later.

@rust-highfive
Copy link
Contributor

r? @nikomatsakis

(rust_highfive has picked a reviewer for you, use r? to override)

@steveklabnik
Copy link
Member Author

oh, and i might not need the channel at the end: https://twitter.com/florob/status/597935275394912259

considering an extra 'cleanup' section

we are going to make some new threads, and that will return some handles to those
threads that let us control their operation. We need to explicitly annotate
the type here, though, due to an issue we’ll talk about later. The `_` is
a type placeholder. We’re saying “`handles` has is a vector of something, but
Copy link
Contributor

Choose a reason for hiding this comment

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

"has is" -> "is"

@steveklabnik
Copy link
Member Author

I believe all nits are addressed. r? @alexcrichton

@Florob
Copy link
Contributor

Florob commented May 12, 2015

So, maybe I'm just terribly dense, but I still don't see why you need the channel at all. AFAICT, it does not become obsolete by joining the threads, but is unnecessary from the get go.

fn done(&self) {
println!("{} is done eating.", self.name);

self.done.send(true).ok().expect("Couldn't finish eating");
Copy link
Member

Choose a reason for hiding this comment

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

I'm still kinda worried about all these .ok().expect(...) calls throughout these examples, some of them I think may be giving the wrong impression. For example this doesn't really indicate anything about "couldn't finish eating" but rather that no one's listening for the notification that the eating is done. Down below there's:

.ok().expect("Couldn't aquire left mutex");

If Err is returned, though, the mutex was indeed acquired, it was just poisoned.

Would it be possible to just use unwrap everywhere? I would consider unwrap as the idiomatic way to indicate that an error should not ever be happening here, and errors should definitely not ever be happening in these examples.

Copy link
Member Author

Choose a reason for hiding this comment

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

I guess I've seen people mostly argue that unwrap gives a poor error message, and ok/expect at least lets you customize it.

that said, I'm sympathetic to the last thing you've said, so yeah, let me re-work it.

@alexcrichton
Copy link
Member

I do also agree with @Florob that the channel involvement here may not be pulling its weight by the time we get to the end.

@steveklabnik
Copy link
Member Author

I added a thing at the end about removing the channel entirely.

@steveklabnik
Copy link
Member Author

@Florob yeah, maybe that is true. sigh.

@steveklabnik
Copy link
Member Author

Okay, I removed the channels entirely, and use unwrap.

Now that we’ve got the basics in place, there’s a number of ways that we can
tackle the broader problem here. I like to start from the end first: let’s
set up a way for each philosopher to finish eating, and report back that
they’re done. As a tiny step, let’s make a method, and then loop through
Copy link
Member

Choose a reason for hiding this comment

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

"and report back that they're done", perhaps drop this phrase?

Copy link
Member Author

Choose a reason for hiding this comment

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

nice catch. editing your own prose is so hard :(

}
```

This `Table` has an array with five elements. These elements are all going
Copy link
Member

Choose a reason for hiding this comment

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

This may need to be rephrased as it's not just a plain vector instead of an array.

Copy link
Member Author

Choose a reason for hiding this comment

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

👍

@alexcrichton
Copy link
Member

Looks good to me! A few last minor nits and otherwise r=me

Nice chapter @steveklabnik!

@steveklabnik
Copy link
Member Author

@bors: r=alexcrichton rollup

@bors
Copy link
Collaborator

bors commented May 12, 2015

📌 Commit 2ba6169 has been approved by alexcrichton

steveklabnik added a commit to steveklabnik/rust that referenced this pull request May 13, 2015
…xcrichton

This is a little rough, and it needs squashed and section headers, but i'd like to get some eyes on it sooner rather than later.
steveklabnik added a commit to steveklabnik/rust that referenced this pull request May 13, 2015
…xcrichton

This is a little rough, and it needs squashed and section headers, but i'd like to get some eyes on it sooner rather than later.
bors added a commit that referenced this pull request May 13, 2015
that the threads complete their work before the program exits.

If you run this program, you’ll see that the philosophers eat out of order!
We have mult-threading!
Copy link
Contributor

Choose a reason for hiding this comment

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

Blegh, I think this is already being tested in a rollup, but maybe there will be a quick chance to fix a couple things soon.

s/mult-threading/multi-threading/

Edit: Never mind, I was missing a paragraph. This is the only nit I found.

Copy link
Member Author

Choose a reason for hiding this comment

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

I'll make a new PR for master, and work it into my backport. Thanks!

@bors bors merged commit 2ba6169 into rust-lang:master May 13, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants