Reputation: 12951
I need to implement (pseudo-code) a new type of compare&swap(a,b) (CAS) object (let's call the new type CAS2) .
CAS and CAS2 objects both support the read operation which returns the object value.
They both support the compare&swap(a,b) operation, but while on CAS this operation return true/false and changes the object value to b if it is equal to a, on CAS2 this operation has the same effect, but instead of returning true/false, it should always return the object value prior to the operation.
For example:
If the CAS object value is 4, compare&swap (4,5) will return true and change the value to 5, but on CAS2 object ,compare&swap (4,5) will return 4 and also change the value to 5 . If the CAS object value is 4, so compare&swap (5,6) will return false and will do nothing , but on CAS2 compare&swap (5,6) will return 4 and also do nothing. The CAS2 object should be implemented using only one CAS object and the implementation should be wait-free and linearizable.
Thanks in advance!
Upvotes: 1
Views: 181
Reputation: 14730
The variant you are requesting is actually the mainsteam one. Implementing such an instruction without mutex requires hardware support, like a dedicated instruction. See the wikipedia article on that topic for further details.
Upvotes: 1