C and C++


CONCURRENCY.DTC : Dynamic Thread Creation

要旨

A program performs a thread creation operation outside the initialization phase.

For the purpose of this check, a thread creation operation is considered to be in the initialization phase if it takes place in a function f() specified with configuration file parameter THREAD_INIT_FUNCTIONS, or in a function directly or transitively called by f().

プロパティ

クラス名 Dynamic Thread Creation
日本語クラス名 Dynamic Thread Creation
クラス分類 スタイル (style)
ニーモニック CONCURRENCY.DTC
カテゴリー
MisraC2023 MisraC2023:D.5.3 There shall be no dynamic thread creation
Misra2012 Misra2012:D.5.3 There shall be no dynamic thread creation
CWE CWE:710 Improper Adherence to Coding Standards
対応言語 C および C++ で利用可能です。
有効/無効設定 このワーニングクラスのチェックはデフォルトで無効になっています。チェックを有効にするにはプロジェクト設定ファイル (configuration file)に以下の WARNING_FILTER ルールを追加してください。
WARNING_FILTER += allow class="Dynamic Thread Creation"

#include <pthread.h>

pthread_t thread1;
pthread_t thread2;

void * thread_func( void * arg ) { return arg; }

/* With factory settings, function name 'dyn_init' matches a
 * THREAD_INIT_FUNCTIONS rule, so this function can directly or
 * transitively call thread creation functions without triggering a
 * 'Dynamic Thread Creation' warning
 */
void dyn_init(){
    if( pthread_create( &thread1, NULL, thread_func, NULL ) ) { thread1 = 0; }
    if( pthread_create( &thread2, NULL, thread_func, NULL ) ) { thread2 = 0; }
}

int main(){
    pthread_t thrd;
    /* starting process - do all thread creation now */
    dyn_init();

    /* no more thread creation from this point on */
    /* ... */
    if( !pthread_create( &thrd, NULL, thread_func, NULL ) )  /* 'Dynamic Thread Creation' warning issued here */
        pthread_detach( thrd );
    /* ... */
    if( thread1 ) pthread_detach( thread1 );
    if( thread2 ) pthread_detach( thread2 );

    return 0;
}

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

Warnings of this class can be triggered by any function specified with configuration file parameter THREAD_CREATION_FUNCTIONS.

注釈

Only code reachable from a program entry point can trigger warnings for this class. CodeSonar will treat a function as a program entry point if it is specified with configuration file parameter PROGRAM_ENTRY_POINTS. The factory setting of this parameter instructs CodeSonar to treat main() and init() as entry points.

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

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