Inflearn コミュニティ Q&A
재진입가능여부에 관한 질문
作成
·
61
1
void swap(int *x, int *y) {
int temp;
temp = *x;
*x = *y;
*y = temp;
}
안녕하세요 강사님 위 함수가 temp를 지역변수로 선언하면 재진입가능한 함수가 된다고하셨는데
x와 y 포인터 접근으로 공유자원이 발생할 수 있는 상황이 발생할 수 있어
재진입 불가능한 함수이지 않나요?
잦은 질문드려 죄송합니다.
クイズ
What is the main reason problems occur with concurrent access to shared variables?
As task priorities differ
Because Context Switching occurs at the C code instruction level
Because Context Switching occurs during a non-atomic operation
The function is called too often
回答 1
1
안녕하세요. 박상우님!
아래와 같이 사용하시면 void swap(int x, int y) 은 재진입에 대응할 수 있는 함수로서의 기능을 잘 수행해줍니다.
// 태스크 1
int a = 10, b = 20;
swap(&a, &b); // temp1은 태스크1의 스택
// 태스크 2 (동시 실행)
int c = 30, d = 40;
swap(&c, &d); // temp2는 태스크2의 스택
하지만, 아래와 같은 사용 예시에서는 재진입이 안된다는 점 양지하시기 바랍니다.
이건 swap 함수가 재진입 불가능해서가 아니라, 호출자가 같은 데이터에 동시 접근했기 때문입니다.
// 태스크 1
swap(&shared_x, &shared_y);
// 태스크 2 (동시 실행)
swap(&shared_x, &shared_y);
- 끝 -





