liveBook · Manning

Thrd_join

As explained in Modern C, Third Edition

thrd_join is a C threads-library function that blocks the calling thread until a specified thread terminates and optionally retrieves its result.

thrd_join synchronizes a calling thread with the termination of a specified thread. The terminating thread’s thrd_exit or return synchronizes with the corresponding thrd_join. section 21.2

Joining worker threads

After main creates several threads, it joins them in a known order. Once main joins the last thread, including the account thread, all effects those threads made on data objects are visible to main. section 21.2

TAKEAWAY:

  • Joining establishes visibility of the terminated thread’s effects; detaching instead removes this thrd_join synchronization point. section 21.2
Table 21.1 C library functions that form synchronization pairs
Release Acquire
thrd_create(.., f, x) Entry to f(x)
thrd_exit by thread id or return from f Start of tss_t destructors for id
End of tss_t destructors for id thrd_join(id) or atexit/at_quick_exit handlers
call_once(&obj, g), first call call_once(&obj, h), all subsequent calls
Mutex release Mutex acquisition

Use the synchronization-pairs table to compare thrd_join with thrd_create, thrd_exit, call_once, and mutex operations. section 21.2

How Modern C, Third Edition covers it

How deep it goes
Explains termination synchronization and transitive visibility, but does not detail call syntax or result retrieval.
Examples
Uses main joining launched threads, including an account thread, to establish ordering and data visibility.
What you can do after
Apply thrd_join to reason about when terminated-thread data effects become visible to the joining thread.
How it connects
Connects thrd_join with thrd_create, thrd_exit, thread return, detachment, atexit, and at_quick_exit.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Modern C, Third Edition. The section links point to the parts of the book it draws on.