C26130

warning C26130: Missing annotation _Requires_lock_held_(<lock>) or _No_competing_thread_ at function <func>. Otherwise it could be a race condition. Variable <var> should be protected by lock <lock>.

Warning C26130 is issued when the analyzer detects a potential race condition but infers that the function is likely to be run in a single threaded mode, for example, when the function is in the initialization stage based on certain heuristics.

Example

In the following example, warning C26130 is generated because a _Guarded_by_ member is being modified without a lock.

    typedef struct _DATA 
    {
        CRITICAL_SECTION cs;
        _Guarded_by_(cs) int data;
    } DATA;

    void Init(DATA* p) 
    {
        p->data = 0; // Warning C26130
    }

If the previous code is guaranteed to be operated in a single threaded mode, annotate the function by using _No_competing_thread_, as shown in the following example.

    typedef struct _DATA 
    {
        CRITICAL_SECTION cs;
        _Guarded_by_(cs) int data;
    } DATA;

    _No_competing_thread_ void Init(DATA* p) 
    {
        p->data = 0; // Warning C26130 will be resolved
    }

Alternatively, you can annotate a code fragment by using _No_competing_thread_begin_ and _No_competing_thread_end_, as follows.

    typedef struct _DATA 
    {
        CRITICAL_SECTION cs;
        _Guarded_by_(cs) int data;
    } DATA;

    void Init(DATA* p) 
    {
        _No_competing_thread_begin_
        p->data = 0; // Warning C26130 will be resolved
        _No_competing_thread_end_
    }