Skip to content

Answer to Exercise 6 of Chapter 7 #32

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 5 commits into from
Apr 24, 2016

Conversation

ssmylh
Copy link
Contributor

@ssmylh ssmylh commented Apr 16, 2016

@axel22 Please review.

@ssmylh ssmylh changed the title Answer to Exercise 6 of Chapter7 Answer to Exercise 6 of Chapter 7 Apr 16, 2016
@annotation.tailrec
final def +=(elem: T): this.type = {
val ts = buf.single()
if (buf.single.compareAndSet(ts, ts :+ elem))
Copy link
Member

Choose a reason for hiding this comment

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

Since you are using STM, you can wrap the two calls into an atomic block, and then you don't need retries if CAS fails. STM should be doing the retries for you.

@axel22
Copy link
Member

axel22 commented Apr 17, 2016

It is certainly useful to have a Vector-based implementation, so I would keep it for instructional purposes and rename it to TVectorBuffer. However, the TArrayBuffer class should use transactions and the TArray class underneath.

@ssmylh
Copy link
Contributor Author

ssmylh commented Apr 17, 2016

@axel22 Thanks for review!
I will fix this (using atomic) and rename this to TVectorBuffer.
In addition, I will try to implement TArrayBuffer using TArray.
Thanks.

@ssmylh
Copy link
Contributor Author

ssmylh commented Apr 24, 2016

@axel22 I renamed TArrayBuffer to TVectorBuffer and fixed this.
Addtionally, I have implemented TArrayBuffer using TArray.
Please review.

@axel22
Copy link
Member

axel22 commented Apr 24, 2016

Great - thanks a lot!

@axel22 axel22 merged commit e9bfbbc into concurrent-programming-in-scala:master Apr 24, 2016
@ssmylh
Copy link
Contributor Author

ssmylh commented Apr 24, 2016

Thanks for merging 😄

@ssmylh ssmylh deleted the ex6-ch7 branch April 24, 2016 09:33
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.

2 participants