TRT
Sign in
mediumAdvancedconcept

What happens when a joinable thread is destroyed without join or detach?

Explain the C++ rule that terminating a joinable thread calls std::terminate, why the rule exists, and how to ensure every thread is joined or detached on all control paths.

TRT practice prompt — not a verified question from a named employer.

The Runtime Theory Team2 min read
Learn it firstLaunching and Joining ThreadsPro — members onlyLaunch threads from five callable forms and join every thread, so ownership, argument passing and lifetime stay correct from the first program.

A strong answer

A strong candidate frames What happens when a joinable thread is destroyed without join or detach? around the exact memory layout, compiler semantics, and hardware execution model covered in Launching and Joining Threads. Explain the C++ rule that terminating a joinable thread calls std::terminate, why the rule exists, and how to ensure every thread is joined or detached on all control paths.

A thread of execution is an independent flow with its own stack and program counter, sharing the address space of the process that created it. C++ starts no worker implicitly. You request one by constructing a thread object with a callable, which is any entity that can be invoked with arguments, plus the arguments for that call. The constructor asks the operating system to start the worker and returns at once, so caller and worker run concurrently from that point on.

A join is the rendezvous where one thread waits for another to finish. Join blocks the caller until the worker function returns, releases the operating system handle, and marks the thread object as non-joinable. Every joinable thread must be joined or detached before its destructor runs, because destroying a joinable thread terminates the program. That strict rule makes lifetime errors loud instead of silent.

In a production interview, always connect the specification rule to its concrete runtime consequence: state what lives on the stack versus the heap or static storage segment, how the compiler or operating system validates alignments, lifetimes, and error codes, and what happens under adversarial or boundary inputs (such as zero-length buffers, integer overflow, concurrent mutation, or partial I/O). Backing your explanation with a small trace of register/memory state transitions immediately separates senior systems engineers from candidates who only memorized syntax.

What the interviewer is listening for

  • Mastery of core invariant: Launch threads from function pointers, lambdas, functors and member functions.
  • Mastery of core invariant: Join every thread and explain detached lifetime risks.
  • Mastery of core invariant: Pass arguments by value or reference wrapper without dangling state.
  • Hardware and ABI awareness: Explaining how sizes, alignments, calling conventions, or kernel boundaries behave on real 64-bit targets rather than relying on implementation-defined assumptions.
  • Defensive engineering: Naming the exact compiler flags (-Wall -Wextra -Werror), static analyzers, or runtime sanitizers (-fsanitize=address,undefined,thread) that verify correctness in CI.

Common mistakes

  • Explaining only surface syntax without tracing the underlying memory addresses, ownership lifetime, or kernel/user-space state transitions.
  • Assuming undefined behavior (such as signed overflow, uninitialized reads, strict-aliasing violations, or dangling pointers) will "just work" because it passed an unoptimized -O0 debug build.
  • Ignoring return codes, partial reads/writes, alignment padding, or cleanup paths when an allocation or syscall fails midway through a function.

This answer walks

Practice follow-ups

  1. 01How does a vector of threads help ensure every worker is joined?
  2. 02Why does detach not solve the destructor problem for most programs?

Not started

Sign in to save your learning progress.

Sign in to save