C and C++ Binaries


CONCURRENCY.LOCALARG : Local Variable Passed to Thread

要旨

A local variable is used as an argument for a thread.

プロパティ

クラス名 Local Variable Passed to Thread
日本語クラス名 Local Variable Passed to Thread
クラス分類 信頼性 (reliability)
ニーモニック CONCURRENCY.LOCALARG
カテゴリー
CERT-C CERT-C:CON31-C Do not destroy a mutex while it is locked
  CERT-C:CON34-C Declare objects shared between threads with appropriate storage durations
CERT-CPP CERT-CPP:CON50-CPP Do not destroy a mutex while it is locked
対応言語 C および C++ で利用可能です。
有効/無効設定 このワーニングクラスのチェックはデフォルトで有効になっています。チェックを無効にするにはプロジェクト設定ファイル (configuration file)に以下の WARNING_FILTER ルールを追加してください。
WARNING_FILTER += discard class="Local Variable Passed to Thread"

#include <pthread.h>

#define NUM_THREADS 5

void *perform_work(void *arguments){
  /* do something with arguments */
  return NULL;
}

int main(void) {
    pthread_t threads[2*NUM_THREADS];
    int local_thread_args[NUM_THREADS];              /* automatic storage duration */
    static int static_thread_args[NUM_THREADS];      /* static storage duration */
    int i;
    int result_code;

    /* pass local data to some threads */
    for (i = 0; i < NUM_THREADS; i++) {
        local_thread_args[i] = i;
        result_code = pthread_create(&threads[2*i], NULL, perform_work, &local_thread_args[i]); /* 'Local Variable Passed to Thread' warning issued here */
        if (result_code != 0) {return result_code;}
    }

    /* pass local-static data to some threads */
    for (i = 0; i < NUM_THREADS; i++) {
        static_thread_args[i] = i;
        result_code = pthread_create(&threads[2*i+1], NULL, perform_work, &static_thread_args[i]);          /* ok: static_thread_args has static storage duration */
        if (result_code != 0) {return result_code;}
    }

    /* wait for threads to complete */
    for (i = 0; i < 2*NUM_THREADS; i++) {
        result_code = pthread_join(threads[i], NULL);
        if (result_code != 0) {return result_code;}
    }

    return 0;
}

ワーニングを引き起こす関数

CodeSonar ships with library models that allow it to a number of functions that pass arguments to threads, across many different libraries. If one of these functions is passed a local variable, a warning will be issued.

If you have created a custom library model for some function f() in terms of one of these existing models, calls to f() will also be capable of triggering Local Variable Passed to Thread warnings.

Functions that can trigger warnings include...
Apache Portable Runtime (APR) apr_thread_create()
ARINC 653 CREATE_PROCESS()
CMX-RTX K_Task_Create()
FreeRTOS xTaskCreate()
libc g_thread_create()
Linux Kernel kernel_thread()
Mac OS X kernel_thread_start()
Win32/MFC AfxBeginThread()
Netscape Portable Runtime (NSPR) PR_CreateThread()
Qt QThread::start()
ThreadX tx_thread_create()
uC/OS-III OSTaskCreate()
VxWorks taskInit()
Win32 CreateThread()
wxWidgets wxThread::Create()

関連のある設定ファイルパラメータ

設定ファイルの以下のパラメータがこのワーニングクラスのチェックに影響します。