C# and Java Operators Thread Safety
Saliya Ekanayake
8:55 PM
C#
,
compound assignment operators
,
concurrency
,
java
,
thread safety
No comments
Java and C# both have compound assignment operators and the increment/decrement operators. For example see http://msdn.microsoft.com/en-us/library/6a71f45d.aspx for a list of C# operators and http://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html for the Java version.It seems these two step operations, i.e. x op= y, x++, x--, ++x, and --x, are NOT thread safe. It makes sense as these operations require reading the current value of the given variable and then updating it with the computed value. It is possible for threads to interleave between these operations resulting erroneous final values.
There are many ways to guarantee the atomicity in such cases including synchronized blocks, atomic data types such as Java AtomicInteger, and interlocked operations like in C#.
Here are some links that may help,
C# interlocked methods: http://msdn.microsoft.com/en-us/library/System.Threading.Interlocked_methods.aspx
Java atomic classes http://docs.oracle.com/javase/6/docs/api/java/util/concurrent/atomic/package-summary.html
C# library to do threading similar to OpenMP like constructs http://msdn.microsoft.com/en-us/library/dd460717.aspx
A Java alternative OpenMP like parallel constructs https://wiki.rice.edu/confluence/display/HABANERO/Habanero+Multicore+Software+Research+Project
Subscribe to:
Posts
(
Atom
)
No comments :
Post a Comment