concurrent-programming-in-scala / concurrent-programming-in-scala.github.io Goto Github PK
View Code? Open in Web Editor NEWWebsite for the Book Learning Concurrent Programming in Scala
Home Page: http://concurrent-programming-in-scala.github.io/
Website for the Book Learning Concurrent Programming in Scala
Home Page: http://concurrent-programming-in-scala.github.io/
[This issue is a clone of this original on the exercises repo of this book ]
I might have found an error. Happy to submit errata @ http://packtpub.com/submit-errata, if I am right.
This concerns a snippet of code on the book, not on the exercises.
On chapter 2, section Volatile variables
, page 53 (on paper print of 1st edition) you have the following snippet of code to illustrate volatile variables:
class Page(val txt: String, var position: Int)
object Volatile extends App {
val pages = for (i <- 1 to 5) yield
new Page("Na" * (100 - 20 * i) + " Batman!", -1)
@volatile var found = false
for (p <- pages) yield thread {
var i = 0
while (i < p.txt.length && !found)
if (p.txt(i) == '!') {
p.position = i
found = true
} else i += 1
}
while (!found) {}
log(s"results: ${pages.map(_.position)}")
}
On the following page, you say:
For the purposes of this example, the main thread busy-waits until it reads found, which is
true. It then prints the positions. Note that a write to position occurs before the write to
found in the spawned threads, which in turn occurs before reading found in the main
thread. This means that the main thread always sees the write of the thread that set found,
and hence prints at least one position other than -1.
This makes total sense.
However, shouldn't the variable position
on class Page
also be volatile?
I am concerned about a scenario on which a spawned thread finds an exclamation point, but the main thread prints the position -1.
Is there anything forbidding the following scenario:
found
to main memory.found
to be true.-1
on the position variable."We consider two threads T1 and T2, which call the releaseCopy method. They both
read the state of the Entry object and create a new state object nstate, which is Idle.
Let's assume that the thread T1 executes the compareAndSet operation first and
returns the old Copying object c from the releaseCopy method. Next, let's assume
that a third thread T3 calls the acquireCopy method and changes the state of the
Entry object to Copying(1). If the thread T1 now calls the acquireCopy method
with the old Copying object c, the state of the Entry object becomes Copying(2).
Note that at this point, the old Copying object c is once again stored inside the atomic
variable state. If the thread T1 now attempts to call compareAndSet, it will succeed
and set the state of the Entry object to Idle. Effectively, the last compareAndSet
operation changes the state from Copying(2) to Idle, so one acquire is lost."
Please correct me If I'm wrong.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.