缺少互斥锁定错误

时间:2013-11-28 06:59:45

标签: c multithreading pthreads mutex coverity

我有一个通过多个线程中的互斥锁访问的变量。

当我对其进行覆盖静态分析时,会出现以下错误: -

MISSING_LOCK(访问变量“g_atag”(g_atag)需要osag_mutex.mutex锁。)[coverity]

代码段:

unsigned long g_atag = 0;
pthread_mutex_t g_atag_lock = PTHREAD_MUTEX_INITIALIZER;

void get_atag(unsigned long *atag)
{
    int ret = -1;

    ret = pthread_mutex_lock(&g_atag_lock);
    if (0 != ret) {
        return;
    }

    if (g_atag < 10000) {
        g_atag++;
    } else {
        g_atag = 0;
    }

    *atag = g_atag;

    pthread_mutex_unlock(&g_atag_lock);
}

有人在这看到任何问题吗?我已经添加了锁,那么为什么说锁缺失?

0 个答案:

没有答案