Main Content

Missing or double initialization of thread attribute

Duplicated initialization of thread attributes or noninitialized thread attribute used in functions that expect initialized attributes

Description

This defect occurs during one of these situations:

  • You initialize a thread attribute twice with a function such as pthread_attr_init without an intermediate call to a function such as pthread_attr_destroy.

    The function pthread_attr_destroy destroys a thread attribute object and enables the system to reclaim resources associated with the object.

  • You use a noninitialized thread attribute in a function such as pthread_create, which expects an initialized attribute. A thread attribute might be noninitialized because it was never initialized previously or destroyed with the pthread_attr_destroy function.

    Noninitialized thread attributes are detected for all functions in the POSIX® standard.

The Result Details pane describes whether the attribute is doubly initialized or noninitialized and also shows previous related events.

Note that a thread attribute is considered initialized only if the call to pthread_attr_init is successful. For instance, the thread attribute is not initialized in the if branch here:

pthread_attr_t attr;
int thread_success;

thread_success = pthread_attr_init(&attr);
if(thread_success != 0) {
   /* Thread attribute considered noninitialized */
}
The issue is also flagged if you do not check the return value from a call to pthread_attr_init.

Risk

Initializing a thread attribute without destroying the previously initialized attribute or using noninitialized thread attributes leads to undefined behavior.

Fix

Before using a thread attribute, initialize the attribute by using the pthread_attr_init function.

pthread_attr_t attr;
int thread_success;

/* Initialize attribute */
thread_success = pthread_attr_init(&attr);
if(thread_success != 0) {
   /* Handle initialization error */
}
...
/* Use attribute */
thread_success = pthread_create(&thr, &attr, &thread_start, NULL);
After initialization, destroy a thread attribute by using pthread_attr_destroy before initializing again:
pthread_attr_t attr;
int thread_success;

/* Destroy attribute */
thread_success = pthread_attr_destroy(&attr);
if(thread_success != 0) {
   /* Handle destruction error */
}
...
/* Reinitialize attribute */
thread_success = pthread_attr_init(&attr);

Examples

expand all

#include <stddef.h>
#include <pthread.h>
#define thread_success 0

extern void *thread_func(void *arg);


int main() {
    pthread_t id;
    pthread_attr_t attr;
    
    if(thread_success == pthread_create(&id, &attr, thread_func, NULL)) {
    }

    return 0;    
}

In this example, the attribute attr is not initialized before its use in the pthread_create call.

Correction – Initialize Thread Attribute Before Use

Before using a thread attribute in the pthread_create function, initialize the attribute with the pthread_attr_init function.

#include <stddef.h>
#include <pthread.h>
#define thread_success 0

extern void *thread_func(void *arg);

int main() {
    pthread_t id;
    pthread_attr_t attr;
    
    if(thread_success != pthread_attr_init(&attr)) {
        return 0;
    }
   
    if(thread_success == pthread_create(&id, &attr, thread_func, NULL)) {
    }
    
    return 0;    
}
#include <stddef.h>
#include <pthread.h>
#define thread_success 0

extern void *thread_func(void *arg);

int main() {
    pthread_t id;
    pthread_attr_t attr;
    
    pthread_attr_init(&attr);
   
    if(thread_success == pthread_create(&id, &attr, thread_func, NULL)) {
    }
    
    return 0;    
}

In this example, the return value of pthread_attr_init is not checked. If the thread attribute initialization fails, the error does not get handled. A possibly undefined thread attribute is later used in the pthread_create function.

Correction – Handle Errors from Thread Attribute Initialization

One possible correction is to use the thread attribute only if the return value from pthread_attr_init indicates successful initialization.

#include <stddef.h>
#include <pthread.h>
#define thread_success 0

extern void *thread_func(void *arg);

int main() {
    pthread_t id;
    pthread_attr_t attr;
    
    if(thread_success != pthread_attr_init(&attr)) {
        return 0;
    }
   
    if(thread_success == pthread_create(&id, &attr, thread_func, NULL)) {
    }
    
    return 0;    
}

Result Information

Group: Concurrency
Language: C
Default: Off
Command-Line Syntax: BAD_THREAD_ATTRIBUTE
Impact: Medium

Version History

Introduced in R2019b