Forums Register Login

How to develop a solution for critical section

+Pie Number of slices to send: Send
Let say we have an atomic function Compare-and-Swap:
CSW(a,b,c)<
if (a==c){
c = b; return 0;
}else { a = c; return 1;}>
How do we use this CSW(a,b,c) to develop a solution to the critical section problem for n threads?
Thank you
+Pie Number of slices to send: Send
A CAS (compare and set) operator is built into the instruction set of almost every processor today (in some form or another). It is atomic. And it is use to implement the basic mutex (synchronization) funntionality.

This is probably a standard homework question for a computer architecture class, so I'll stop here. How do you think it works? Given the capability to conditionally set or swap a value, based on its current value, how would you implement synchronization?


BTW, as a side note, the Java Virtual Machine actually doesn't have a CAS in its instruction set. Instead, it implements the higher level monitor in and out (synchronization) directly. I guess this is so that it can do synchronization with one bytecode instruction.

Unfortunately, without a CAS, you have to rely on synchronization for everything, making it impossible to code optimistically. This is why they added the Atomic Variables in Java 5 -- this is basically a set of variables that implements a form of CAS (obviously using JNI under the covers to get to the CAS operator).

Henry
Squanch that. And squanch this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 850 times.
Similar Threads
Another Mock Exam Question
SCJP1.5
Tiger objectives - take 2
PhraseOMatic Errors From Head First Java (Help! Please Help! Sobs...)
Synchronization & Locks.
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 13:45:10.