Skip to content

Latest commit

 

History

History
77 lines (58 loc) · 1.97 KB

c26100.md

File metadata and controls

77 lines (58 loc) · 1.97 KB
descriptiontitlems.datef1_keywordshelpviewer_keywordsms.assetid
Learn more about: Warning C26100
Warning C26100
11/04/2016
C26100
RACE_CONDITION
__WARNING_RACE_CONDITION
C26100
470ab2b2-5b55-424f-b192-3863a773c892

Warning C26100

Race condition. Variable 'var' should be protected by lock 'lock'.

Remarks

The _Guarded_by_ annotation in the code specifies the lock to use to guard a shared variable. Warning C26100 is generated when the guard contract is violated.

Code analysis name: RACE_CONDITION

Examples

The following example generates warning C26100 because there's a violation of the _Guarded_by_ contract.

CRITICAL_SECTION gCS; _Guarded_by_(gCS) int gData; typedefstruct_DATA { _Guarded_by_(cs) int data; CRITICAL_SECTION cs; } DATA; voidSafe(DATA* p) { EnterCriticalSection(&p->cs); p->data = 1; // OKLeaveCriticalSection(&p->cs); EnterCriticalSection(&gCS); gData = 1; // OKLeaveCriticalSection(&gCS); } voidUnsafe(DATA* p) { EnterCriticalSection(&p->cs); gData = 1; // Warning C26100 (wrong lock)LeaveCriticalSection(&p->cs); }

The contract violation occurs because an incorrect lock is used in the function Unsafe. In this case, gCS is the correct lock to use.

Occasionally a shared variable only has to be guarded for write access but not for read access. In that case, use the _Write_guarded_by_ annotation, as shown in the following example.

CRITICAL_SECTION gCS; _Guarded_by_(gCS) int gData; typedefstruct_DATA2 { _Write_guarded_by_(cs) int data; CRITICAL_SECTION cs; } DATA2; intSafe2(DATA2* p) { // OK: read does not have to be guardedint result = p->data; return result; } voidUnsafe2(DATA2* p) { EnterCriticalSection(&gCS); // Warning C26100 (write has to be guarded by p->cs) p->data = 1; LeaveCriticalSection(&gCS); }

This example also generates warning C26100 because it uses an incorrect lock in the function Unsafe2.

close