These are chat archives for rust-lang/rust

3rd
Jun 2016
Redpix_
@redpixr
Jun 03 2016 17:16
Hey Rustaceans! How are you all doing? I have a quick question. I was reading rust book just now and there's a dining philosophers problem about concurrency and threading. I noticed that the output in the book at the end without forks was pretty different from what I've got running in my terminal here it is:
Gilles Deleuze is eating.
Gilles Deleuze is done eating.
Friedrich Nietzsche is eating.
Friedrich Nietzsche is done eating.
Michel Foucault is eating.
Baruch Spinoza is eating.
Baruch Spinoza is done eating.
Karl Marx is eating.
Karl Marx is done eating.
Michel Foucault is done eating.
This one from my computer:
Baruch Spinoza is eating.
Gilles Deleuze is eating.
Karl Marx is eating.
Friedrich Nietzsche is eating.
Michel Foucault is eating.
Baruch Spinoza is done eating.
Gilles Deleuze is done eating.
Karl Marx is done eating.
Friedrich Nietzsche is done eating.
Michel Foucault is done eating.
Does anybody know what's going on here?
Denis Lisov
@tanriol
Jun 03 2016 17:35
@redpixr, the second output certainly looks OK for the case without forks.
Redpix_
@redpixr
Jun 03 2016 17:38
@tanriol Yeah I thought of that because eat prints at first then sleeps for a second and prints that eating is done and I think it should be in order but is it because that the threads were run in another order on the book author's machine? My output is going in the order that is in the vector.
Denis Lisov
@tanriol
Jun 03 2016 17:44
@redpixr, I'd assume that the output was simply made up as it seems a bit too unrealistic... except maybe if there was no sleep in the program.
Redpix_
@redpixr
Jun 03 2016 17:45
@tanriol Which output mine or one that is in the book?
LeonineKing1199
@LeonineKing1199
Jun 03 2016 17:47

A data race is any unsynchronized, concurrent access to data involving a write.

I saw this in the Rust docs and I'm pretty sure that this is only half-complete. As far as I'm aware, a thread is free to write to a location while another thread is reading from it.

Edit: It should read, "A data race is any unsynchronized, concurrent access to data."

Redpix_
@redpixr
Jun 03 2016 17:48
@tanriol There's thread::sleep_ms(1000); in the book and Rust compiler warned me that it's deprecated and I went for thread::sleep(Duration::new(1, 0)); 1-secs, 0-millisecs
Denis Lisov
@tanriol
Jun 03 2016 17:51
@redpixr, and BTW, I don't see the example in the current Rust book (since 1.7.0 at least). Are you sure you're studying a current one?
As far as I'm aware, a thread is free to write to a location while another thread is reading from it.
In safe Rust it's generally not. Only one of threads can have mutable reference to a memory location.
LeonineKing1199
@LeonineKing1199
Jun 03 2016 17:54
If only one thread can have a mutable reference, the rest can have constant references. That still doesn't make it safe as one thread is still free to mutate while others read.
Redpix_
@redpixr
Jun 03 2016 17:54
@tanriol Wow I haven't looked at the version. I just typed rust book pdf yesterday and it appears that there's no dining philosophers example in new browser based one after number guessing.
Denis Lisov
@tanriol
Jun 03 2016 17:55
And while it has the mutable reference no other references may exist (and be usable).
LeonineKing1199
@LeonineKing1199
Jun 03 2016 17:55
Ah, I see. That's pretty prohibitive. But I guess it does enforce safety. And the programming style that parallelized code should be reading from its own private buffer.
But that prevents a lot of shared memory access that can easily be facilitated by spinlocks or mutexes.
Erik Hedvall
@Ogeon
Jun 03 2016 17:57
That rule is like a compile time mutex (or rather a read/write lock). There are fully functional mutexes and spin locks as well
LeonineKing1199
@LeonineKing1199
Jun 03 2016 17:58
And the compiler can differentiate between the hard-encoded rule and runtime constructs?
Denis Lisov
@tanriol
Jun 03 2016 17:59
The actual Mutex type provides the possibility to 'upgrade' a shared readonly reference to a mutable one. It explicitly uses synchronization and ensures that there's only one and no other readonly references.
LeonineKing1199
@LeonineKing1199
Jun 03 2016 18:01
And I'm assuming that's for the lifetime of the code that relies on the mutative code.
Erik Hedvall
@Ogeon
Jun 03 2016 18:02
Yes, it returns a handle that represents the borrow, and the lock is released when that handle goes out of scope. Here are the docs, in case you want to look at them: https://doc.rust-lang.org/std/sync/struct.Mutex.html
LeonineKing1199
@LeonineKing1199
Jun 03 2016 18:03
I feel like that's a really intense way of introducing parallelism.
Redpix_
@redpixr
Jun 03 2016 18:04
Is this how it goes? So we spawn some threads and collect makes sure that after finishing each thread its handle is returned and then we join them?
@LeonineKing1199 same here :D I couldn't get it quiet yet I think
LeonineKing1199
@LeonineKing1199
Jun 03 2016 18:05
Granted, the added safety might be worth it, even through it seems to me that there's a lot of bending over backwards to make the Rust compiler happy.
Erik Hedvall
@Ogeon
Jun 03 2016 18:13
It's usually not in your way, but yes, there are moments when it feels very restricting. I haven't used any synchronization in other languages, recently, so I can't really compare. It's at least quite fearless in Rust, so that's something. :smile:
LeonineKing1199
@LeonineKing1199
Jun 03 2016 18:14
I've done a lot of parallel stuff before and I like that Rust is good at enforcing thread-safety (this is huge, tbh) but I'd take a different approach that I would consider "simpler".
Erik Hedvall
@Ogeon
Jun 03 2016 18:16
The thread safety, which partially depends on those unique mutable reference rules, are really clever. That's what makes things really nice
Redpix_
@redpixr
Jun 03 2016 19:00
What is the real difference between let x = 5; x = 10; and let mut x = 5; x = 10;. This one compiles and I also checked the Rust compilers nice explanation (Which is BTW super cool) but what is going underneath can anybody tell me? Why does that mut makes variable mutable and why not just rebinding is possible with let x = 5; x = 10;? Also if I write mut what's the underlying difference how are those handled differently?
Redpix_
@redpixr
Jun 03 2016 19:07
Is it like const compared to some other languages?
Sean Perry
@shaleh
Jun 03 2016 19:09
pretty much. let x = 5 can essentially be compiled away whereas let mut x = 5 means the compiler has to watch for and track assignments using registers, etc.
Redpix_
@redpixr
Jun 03 2016 19:12
@shaleh Thanks! :+1:
Risto Stevcev
@Risto-Stevcev
Jun 03 2016 22:04
hey @LeonineKing1199 :) It's funny how I run into Ramda/Sanctuary people in other FP rooms, hehe
LeonineKing1199
@LeonineKing1199
Jun 03 2016 22:06
Ha! I'm trying to pick up on some Rust to see what it's capable of.
Risto Stevcev
@Risto-Stevcev
Jun 03 2016 22:06
hehe, yeah same here, but I'm prioritizing purescript first
have you checked out Erlang for concurrency? I heard that the actors are automatically partitioned into threads or processes (forgot exactly what it was)
but that it was generally made really well for scaling and clustering
LeonineKing1199
@LeonineKing1199
Jun 03 2016 22:07
I've been thinking about using some "obscure" languages like that XD

I really want to learn Haskell but at the same time, I really don't. It's a weird thing I have going on right now.

My current focus is CUDA though. I'm trying to really sit down and work on GPGPU stuff again.

Risto Stevcev
@Risto-Stevcev
Jun 03 2016 22:09
yeah rust sounds like a good fit for that
I'd like to start writing some kernel modules with rust, and maybe eventually a rewrite of minix3 in rust so that people can use it for embedded devices
Purescript kind of fixed some of the annoying stuff about haskell, like strict evaluation, no cabal, explicit forall, row polymorphism...
One thing that I find really annoying is the insistence on using obscure symbols to denote functions (like * <<< <$> $). You can look them up on Hoogle or Pursuit, but if those aren't giving you the answer you want, you'll be shit out of luck
They aren't exactly easily searchable
Risto Stevcev
@Risto-Stevcev
Jun 03 2016 22:14
And they don't give you any initial clues as to what they do... definitely something they took from math that makes no sense in programming
LeonineKing1199
@LeonineKing1199
Jun 03 2016 22:15
Yeah, I think Ramda is actually the "best" when it comes to integrating FP into CS

I think the whole goal is to get programmers to think of functions as variables that you can compose and pass around with. But once you get to algebraic data types, I think you lose a lot of the "usefulness", so to speak.

But that's just how I see it.

Sean Perry
@shaleh
Jun 03 2016 22:16
The symbol names let people write really dense code which some find fun
definitely not intuitive for the rest of us
LeonineKing1199
@LeonineKing1199
Jun 03 2016 22:18
If your code is dense enough to trigger the formation of a black hole, you've gone too far.
Erik Hedvall
@Ogeon
Jun 03 2016 22:29
Some say that a black hole may be the perfect or most efficient computer
Risto Stevcev
@Risto-Stevcev
Jun 03 2016 22:32
lol
I prefer my code to read as close to english as possible. Having to parse a symbol in your head just adds extra overhead to whatever algo you're working on
LeonineKing1199
@LeonineKing1199
Jun 03 2016 22:34
* is joking * If that was the goal, we'd all be using gotos. It makes so much sense in English though, you're just like, "Hey, let's goto In 'n' Out"
Risto Stevcev
@Risto-Stevcev
Jun 03 2016 22:35
lol
I like ADTs though. It's just basically like JSON + enums combined, that you can destructure and pattern-match against.
but i'll admit that you don't really need them
Sean Perry
@shaleh
Jun 03 2016 22:37
ADTs in a strongly typed language enabled some nice patterns
Risto Stevcev
@Risto-Stevcev
Jun 03 2016 22:38
yeah def
Sean Perry
@shaleh
Jun 03 2016 22:39
there is a cool demo from a while back using shell commands and sudo. A sudo command was a different type so you could not accidentally call 'rm' with it.
I was just googling and not seeing it
Risto Stevcev
@Risto-Stevcev
Jun 03 2016 22:41
oh that's cool
Sean Perry
@shaleh
Jun 03 2016 22:42
I like ADTs for that kind of thing. "ParsedString" vs. String. So You have to pass a String through parseInput before you can use it.
Risto Stevcev
@Risto-Stevcev
Jun 03 2016 22:42
I'm wondering what's stopping people from writing a more type-safe shell like that. All shell programs are (bash, ash, etc) are just repls
Sean Perry
@shaleh
Jun 03 2016 22:43
usability mostly
Risto Stevcev
@Risto-Stevcev
Jun 03 2016 22:43
it could be just as useable
I think I'm not the only one who has accidentally ran commands out of being too tired or not paying attention that I later regretted
Sean Perry
@shaleh
Jun 03 2016 22:44
no kidding
Risto Stevcev
@Risto-Stevcev
Jun 03 2016 22:44
it would be nice to have something that's stricter than that, but not so strict that you have to solve the hellraiser puzzle to do anything useful