C and C++


CONCURRENCY.DTC : Dynamic Thread Creation

Summary

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().

Properties

Class Name Dynamic Thread Creation
Significance style
Mnemonic CONCURRENCY.DTC
Categories
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
Availability Available for C and C++.
Enabling Checks for this warning class are disabled by default. To enable them, add the following WARNING_FILTER rule to the project configuration file.
WARNING_FILTER += allow class="Dynamic Thread Creation"

Example

#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;
}

Triggering Functions

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

Notes

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.

Relevant Configuration File Parameters

The following configuration file parameters affect checks for this warning class.