X
xmarlawx
Hello guys,
I have to implement the Peterson's algorithm in java.
For who of you which is not aware about it, is a low level concurrency
solution for two threads
that want to use a shared resource.
The algorithm use three variables, two flags (booleans) and a turn
indicator (usually implemented as an int, but possibly a boolean would
work as well).
More information about the algorithm here :
http://en.wikipedia.org/wiki/Peterson's_algorithm
Now, is really easy to implement it and in theory I won't need to use
any synchronization
construct available in java (such as synchronized keyword) since the
shared memory should be enough.
Here comes my problem : I learned that some hardware system don't
necessarily execute the instructions
in a FIFO order, such as, they don't execute necessarily instructions
in the same order as they are written in the source code (and then
compiled) to improve efficency.
All this in a standard sequencial program (not concurrent).
Such hardware, to support concurrency, has special "atomic" instrucion
such as test & set.
What about the jvm ?
Because if this is the case (such as execution of the instructions not
in a sequencial way), the instructions in my program could be executed
in an order that is not the one I wrote it that will eventually result
in an invalid state of the program.
Thank you for any information.
I have to implement the Peterson's algorithm in java.
For who of you which is not aware about it, is a low level concurrency
solution for two threads
that want to use a shared resource.
The algorithm use three variables, two flags (booleans) and a turn
indicator (usually implemented as an int, but possibly a boolean would
work as well).
More information about the algorithm here :
http://en.wikipedia.org/wiki/Peterson's_algorithm
Now, is really easy to implement it and in theory I won't need to use
any synchronization
construct available in java (such as synchronized keyword) since the
shared memory should be enough.
Here comes my problem : I learned that some hardware system don't
necessarily execute the instructions
in a FIFO order, such as, they don't execute necessarily instructions
in the same order as they are written in the source code (and then
compiled) to improve efficency.
All this in a standard sequencial program (not concurrent).
Such hardware, to support concurrency, has special "atomic" instrucion
such as test & set.
What about the jvm ?
Because if this is the case (such as execution of the instructions not
in a sequencial way), the instructions in my program could be executed
in an order that is not the one I wrote it that will eventually result
in an invalid state of the program.
Thank you for any information.