[concurrency-interest] On A Formal Definition of 'Data-Race'

Nathan Reynolds nathan.reynolds at oracle.com
Thu Apr 18 13:05:10 EDT 2013


In the future, HotSpot will probably use Intel TSX for synchronized 
blocks.  Harmless data races such as String.hashCode() can now cause 
transaction aborts.  For example, all of the threads/cores compute the 
value, but only 1 thread sets the field.  The rest of the threads/cores 
see the field set and have to roll back their transactions and start 
over.  This is going to waste all of the cycles spent executing from the 
beginning of the synchronized block to the point of calling 
String.hashCode().  Wrapping String.hashCode() in its own synchronized 
block isn't going to work since the abort goes to the start of the outer 
most transaction.  The only solution I can think of is calling 
String.hashCode() just before the synchronized block.  But, I would only 
recommend this if enough cycles are wasted from calling String.hashCode().

Nathan Reynolds 
<http://psr.us.oracle.com/wiki/index.php/User:Nathan_Reynolds> | 
Architect | 602.333.9091
Oracle PSR Engineering <http://psr.us.oracle.com/> | Server Technology
On 4/16/2013 12:22 PM, Vitaly Davidovich wrote:
>
> String caches the hashcode in a plain field; multiple threads can race 
> to compute and store the hashcode, but since string is immutable, this 
> is fine.  The code is careful to operate only on a local variable to 
> avoid re-reads of the field (which could theoretically lead to wrong 
> results if, e.g., JIT reordered code in a certain way).
>
> On Apr 16, 2013 3:12 PM, "thurstonn" <thurston at nomagicsoftware.com 
> <mailto:thurston at nomagicsoftware.com>> wrote:
>
>     Just curious, how is String#hashCode() racy?
>     Strings are immutable in java; I looked at the code a bit and I
>     didn't see
>     anything that looked racy.
>     The only thing I guess could be:
>     private char[] value
>
>
>     Although that array is never modified in the String class, so . . .
>
>
>
>     --
>     View this message in context:
>     http://jsr166-concurrency.10961.n7.nabble.com/On-A-Formal-Definition-of-Data-Race-tp9408p9445.html
>     Sent from the JSR166 Concurrency mailing list archive at Nabble.com.
>     _______________________________________________
>     Concurrency-interest mailing list
>     Concurrency-interest at cs.oswego.edu
>     <mailto:Concurrency-interest at cs.oswego.edu>
>     http://cs.oswego.edu/mailman/listinfo/concurrency-interest
>
>
>
> _______________________________________________
> Concurrency-interest mailing list
> Concurrency-interest at cs.oswego.edu
> http://cs.oswego.edu/mailman/listinfo/concurrency-interest

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://cs.oswego.edu/pipermail/concurrency-interest/attachments/20130418/8705aa99/attachment.html>


More information about the Concurrency-interest mailing list