Main Content

Join or detach of a joined or detached thread

Thread that was previously joined or detached is joined or detached again

Description

This checker is deactivated in a default Polyspace® as You Code analysis. See Checkers Deactivated in Polyspace as You Code Analysis (Polyspace Access).

This defect occurs when:

  • You try to join a thread that was previously joined or detached.

  • You try to detach a thread that was previously joined or detached.

The Result Details pane describes if the thread was previously joined or detached and also shows previous related events.

For instance, a thread joined with thrd_join is then detached with thrd_detach:

thrd_t id;
...
thrd_join(id, NULL);
thrd_detach(id);

Note that a thread is considered as joined only if a previous thread joining is successful. For instance, the thread is not considered as joined in the if branch here:

thrd_t t;
...
if (thrd_success != thrd_join(t, 0)) {
    /* Thread not considered joined */
}
The analysis cannot detect cases where a joined thread detaches itself using, for instance, the thrd_current() function.

Risk

The C11 standard (clauses 7.26.5.3 and 7.26.5.6) states that a thread shall not be joined or detached once it was previously joined or detached. Violating these clauses of the standard results in undefined behavior.

Fix

Avoid joining a thread that was already joined or detached previously. Avoid detaching a thread that was already joined or detached.

Examples

expand all

#include <stddef.h>
#include <threads.h>
#include <stdlib.h>


extern int thread_func(void *arg);

int main (void)
{
  thrd_t t;

  if (thrd_success != thrd_create (&t, thread_func, NULL)) {
    /* Handle error */
    return 0;
  }

  if (thrd_success != thrd_join (t, 0)) {   
    /* Handle error */
    return 0;
  }

  if (thrd_success != thrd_detach (t)) {   
    /* Handle error */
    return 0;
  }

  return 0;
}

In this example, the use of thrd_detach on a thread that was previously joined with thrd_join leads to undefined behavior.

To avoid compilation errors when running Bug Finder on this example, specify the C11 standard with the option C standard version (-c-version).

Correction – Avoid Detaching a Joined Thread

Remove the prior thrd_join or the subsequent thrd_detach statement. In this corrected version, the thrd_detach statement is removed.

#include <stddef.h>
#include <threads.h>
#include <stdlib.h>


extern int thread_func(void *arg);

int main (void)
{
  thrd_t t;

  if (thrd_success != thrd_create (&t, thread_func, NULL)) {
    /* Handle error */
    return 0;
  }

  if (thrd_success != thrd_join (t, 0)) {   
    /* Handle error */
    return 0;
  }

  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;
    
    if(thread_success != pthread_attr_init(&attr)) {
        return 0;
    }
   
    if(thread_success != pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED)) {
        return 0;
    }
       
    if(thread_success != pthread_create(&id, &attr, thread_func, NULL)) {
            return 0;
    }
    
    if(thread_success != pthread_join(id, NULL)) {
            return 0;
    }

    return 0;    
}

In this example, the thread attribute is assigned the state PTHREAD_CREATE_DETACHED. A thread created using this attribute is then joined.

Correction – Create Threads as Joinable

One possible correction is to create a thread with thread attribute assigned to the state PTHREAD_CREATE_JOINABLE and then join the thread.

#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_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE)) {
        return 0;
    }
       
    if(thread_success != pthread_create(&id, &attr, thread_func, NULL)) {
            return 0;
    }
    
    if(thread_success != pthread_join(id, NULL)) {
            return 0;
    }

    return 0;    
}

Result Information

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

Version History

Introduced in R2019b