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 aspthread_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 thepthread_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 */ }
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);
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
Result Information
Group: Concurrency |
Language: C |
Default: Off |
Command-Line Syntax:
BAD_THREAD_ATTRIBUTE |
Impact: Medium |
Version History
Introduced in R2019b
See Also
Use of undefined thread
ID
| Join or detach of a joined or detached
thread
Topics
- Interpret Bug Finder Results in Polyspace Desktop User Interface
- Interpret Bug Finder Results in Polyspace Access Web Interface (Polyspace Access)
- Address Results in Polyspace User Interface Through Bug Fixes or Justifications
- Address Results in Polyspace Access Through Bug Fixes or Justifications (Polyspace Access)