C26100
경고 C26100: 경합 상태변수 <var>은 lock <lock>으로 보호해야 합니다.
_Guarded_by_ 주석 코드에서 잠금을 공유 변수를 사용 하 여 지정 합니다.경고 C26100 가드 계약이 위반 될 때 생성 됩니다.
예제
다음 예제에서는 경고가 C26100 위반 때문에 _Guarded_by_ 계약을 일반화 합니다.
CRITICAL_SECTION gCS;
_Guarded_by_(gCS) int gData;
typedef struct _DATA {
_Guarded_by_(cs) int data;
CRITICAL_SECTION cs;
} DATA;
void Safe(DATA* p) {
EnterCriticalSection(&p->cs);
p->data = 1; // OK
LeaveCriticalSection(&p->cs);
EnterCriticalSection(&gCS);
gData = 1; // OK
LeaveCriticalSection(&gCS);
}
void Unsafe(DATA* p) {
EnterCriticalSection(&p->cs);
gData = 1; // Warning C26100 (wrong lock)
LeaveCriticalSection(&p->cs);
}
잘못 된 잠금 함수에서 사용 되기 때문에 계약 위반 발생 Unsafe 합니다.이 경우 gCS 를 사용 하 여 적절 한 잠금입니다.
가끔 공유 변수는만 읽기 권한이 아니라 쓰기 액세스에 대 한 보호합니다. _Write_guarded_by_ 주석, 다음 예제와 같이 사용하세요.
CRITICAL_SECTION gCS;
_Guarded_by_(gCS) int gData;
typedef struct _DATA2 {
_Write_guarded_by_(cs) int data;
CRITICAL_SECTION cs;
} DATA2;
int Safe2(DATA2* p) {
// OK: read does not have to be guarded
int result = p->data;
return result;
}
void Unsafe2(DATA2* p) {
EnterCriticalSection(&gCS);
// Warning C26100 (write has to be guarded by p->cs)
p->data = 1;
LeaveCriticalSection(&gCS);
}
이 예제에서는 경고도 생성 C26100 함수에서 잘못 된 잠금 사용 하기 때문에 Unsafe2 생성합니다.