elast0ny / shared_memory

A Rust wrapper around native shared memory for Linux and Windows
382 stars 51 forks source link

Update mutex example to be more meaningful #56

Closed xixixao closed 3 years ago

xixixao commented 3 years ago

Without releasing the lock the example doesn't give very interesting output.

elast0ny commented 3 years ago

Hi, the original mutex example is made this way to highlight the "unlock on drop" behavior. The lock will be held for as long as guard is in scope, which means the lock will be held for at least 1 second (because of the thread::sleep). This should make it nice and easy to see each thread exchanging the lock back and forth when running the example. If this is not what happens when you run the example, then an issue should be filed.

This is the same behavior as regular std's MutexGuard.

elast0ny commented 3 years ago

I've updated the example so that you only have to run one instance of it and it spawns threads (instead of relying on users spawning two instances of the example).

Hopefully this clarifies things for you and future users

xixixao commented 3 years ago

I think the example was clearer before (at least closer to what I'm using this library for).

If this is not what happens when you run the example, then an issue should be filed.

This is not what happens. I don't think Rust drops before the sleep.

xixixao commented 3 years ago

I think you did fix it in the new code with the inner scope though.