• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

How to develop a solution for critical section

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Look! It's Leonardo da Vinci! And he brought a tiny ad!
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic