워드의 내용을 검사하고 변경하거나, 두 워드의 내용을 원자적으로 교환할 수 있는데, 이를 할 수 있도록 도와주는 하드웨어딴의 명령어
test_and_set
/* 여기에 특정 조건을 더해 검사하고True False 설정 변경을 가능케한다 */
boolean test_and_set(boolean *target){
boolean rv = *target; /* rv 값에 target 값 대입 */
*target = true; /* target 값 변경 후 반환 */
return rv;
}
=> 세 문장 보두 한 번에 순차적으로 수행
/* lock이라는 공동 변수를 사용 */
do{
while(test_and_set(&lock)){
;/* do nothing */
/* critical section -> 수행중에 아마 lock = true로 또 설정할 듯*/
lock = false;
/* remainder section */
} while(true)
compare_and_swap(CAS)
int compare_and_swap(int *value, int expected, int new_value){
int temp = *value;
if(*value == expected){
value = new_value;
}
return temp;
}
Atomic Variable (원자적 변수)
increment(&sequence) -> 단순한 증감 함수
void increment(atomic int *v){
int temp;
do{
temp = *v;
}
while(temp != compare_and_swap(v, temp, temp+1);
}