liveBook · Manning

Thrd_t

As explained in Modern C

thrd_t is the C standard threads type that identifies a thread through an implementation-defined opaque representation.

A C thread is identified by a thrd_t value, an opaque, implementation-defined representation. The value is enough to manage the thread; threads are not hierarchically organized. section 18.0 section 18.6

Collecting thread identifiers

thrd_t thrd[4]; thrd_create(&thrd[0], update_thread, &L); thrd_create(&thrd[1], draw_thread, &L); thrd_join(thrd[0], 0); thrd_join(thrd[1], 0); The program stores the identifiers returned through thrd_create and passes them to thrd_join. Code need not inspect the representation of thrd_t. section 18.0 section 18.0

TAKEAWAY: choose thread-identifier handling according to the thread's lifetime.

  • Join a thread when the program waits for its termination and needs to manage its completion. section 18.0
  • Detach a thread when nobody will wait for it; an unjoined dead thread retains resources until execution ends. section 18.6
  • Use thrd_current to obtain the running thread's identifier, then pass that thrd_t to thread-management operations. section 18.6
210
211   void B9_atexit(void) {
212     /* Puts the board in a nice final picture */
213     L.iteration = L.last;
214     life_draw(&L);
215     life_destroy(&L);
216   }
217
218   int main(int argc, char* argv[argc+1]) {
219     /* Uses command-line arguments for the size of the board */
220     size_t n0 = 30;
221     size_t n1 = 80;
222     if (argc > 1) n0 = strtoull(argv[1], 0, 0);
223     if (argc > 2) n1 = strtoull(argv[2], 0, 0);
224     /* Create an object that holds the game's data. */
225     life_init(&L, n0, n1, M);
226     atexit(B9_atexit);
227     /* Creates four threads that operate on the same object and
228        discards their IDs */
229     thrd_create(&(thrd_t){0}, update_thread,  &L);
230     thrd_create(&(thrd_t){0}, draw_thread,    &L);
231     thrd_create(&(thrd_t){0}, input_thread,   &L);
232     /* Ends this thread nicely and lets the threads go on nicely */
233     thrd_exit(0);
234   }


!@%STYLE%@!
{"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":0,\"ch\":0},{\"line\":0,\"ch\":3}],[{\"line\":1,\"ch\":0},{\"line\":1,\"ch\":3}],[{\"line\":1,\"ch\":6},{\"line\":1,\"ch\":10}],[{\"line\":1,\"ch\":6},{\"line\":1,\"ch\":10}],[{\"line\":2,\"ch\":0},{\"line\":2,\"ch\":3}],[{\"line\":3,\"ch\":0},{\"line\":3,\"ch\":3}],[{\"line\":4,\"ch\":0},{\"line\":4,\"ch\":3}],[{\"line\":5,\"ch\":0},{\"line\":5,\"ch\":3}],[{\"line\":6,\"ch\":0},{\"line\":6,\"ch\":3}],[{\"line\":7,\"ch\":0},{\"line\":7,\"ch\":3}],[{\"line\":8,\"ch\":0},{\"line\":8,\"ch\":3}],[{\"line\":8,\"ch\":6},{\"line\":8,\"ch\":9}],[{\"line\":8,\"ch\":10},{\"line\":8,\"ch\":14}],[{\"line\":8,\"ch\":6},{\"line\":8,\"ch\":9}],[{\"line\":8,\"ch\":25},{\"line\":8,\"ch\":29}],[{\"line\":9,\"ch\":0},{\"line\":9,\"ch\":3}],[{\"line\":10,\"ch\":0},{\"line\":10,\"ch\":3}],[{\"line\":10,\"ch\":8},{\"line\":10,\"ch\":14}],[{\"line\":11,\"ch\":8},{\"line\":11,\"ch\":14}],[{\"line\":11,\"ch\":0},{\"line\":11,\"ch\":3}],[{\"line\":10,\"ch\":8},{\"line\":10,\"ch\":14}],[{\"line\":11,\"ch\":8},{\"line\":11,\"ch\":14}],[{\"line\":12,\"ch\":0},{\"line\":12,\"ch\":3}],[{\"line\":4,\"ch\":9},{\"line\":4,\"ch\":11}],[{\"line\":5,\"ch\":9},{\"line\":5,\"ch\":11}],[{\"line\":12,\"ch\":8},{\"line\":12,\"ch\":10}],[{\"line\":13,\"ch\":8},{\"line\":13,\"ch\":10}],[{\"line\":15,\"ch\":9},{\"line\":15,\"ch\":11}],[{\"line\":12,\"ch\":27},{\"line\":12,\"ch\":35}],[{\"line\":13,\"ch\":27},{\"line\":13,\"ch\":35}],[{\"line\":13,\"ch\":0},{\"line\":13,\"ch\":3}],[{\"line\":4,\"ch\":9},{\"line\":4,\"ch\":11}],[{\"line\":5,\"ch\":9},{\"line\":5,\"ch\":11}],[{\"line\":12,\"ch\":8},{\"line\":12,\"ch\":10}],[{\"line\":13,\"ch\":8},{\"line\":13,\"ch\":10}],[{\"line\":15,\"ch\":9},{\"line\":15,\"ch\":11}],[{\"line\":12,\"ch\":27},{\"line\":12,\"ch\":35}],[{\"line\":13,\"ch\":27},{\"line\":13,\"ch\":35}],[{\"line\":14,\"ch\":0},{\"line\":14,\"ch\":3}],[{\"line\":15,\"ch\":0},{\"line\":15,\"ch\":3}],[{\"line\":16,\"ch\":0},{\"line\":16,\"ch\":3}],[{\"line\":1,\"ch\":14},{\"line\":1,\"ch\":20}],[{\"line\":16,\"ch\":8},{\"line\":16,\"ch\":14}],[{\"line\":17,\"ch\":0},{\"line\":17,\"ch\":3}],[{\"line\":18,\"ch\":0},{\"line\":18,\"ch\":3}],[{\"line\":19,\"ch\":0},{\"line\":19,\"ch\":3}],[{\"line\":19,\"ch\":8},{\"line\":19,\"ch\":19}],[{\"line\":20,\"ch\":8},{\"line\":20,\"ch\":19}],[{\"line\":21,\"ch\":8},{\"line\":21,\"ch\":19}],[{\"line\":19,\"ch\":22},{\"line\":19,\"ch\":28}],[{\"line\":20,\"ch\":22},{\"line\":20,\"ch\":28}],[{\"line\":21,\"ch\":22},{\"line\":21,\"ch\":28}],[{\"line\":20,\"ch\":0},{\"line\":20,\"ch\":3}],[{\"line\":19,\"ch\":8},{\"line\":19,\"ch\":19}],[{\"line\":20,\"ch\":8},{\"line\":20,\"ch\":19}],[{\"line\":21,\"ch\":8},{\"line\":21,\"ch\":19}],[{\"line\":19,\"ch\":22},{\"line\":19,\"ch\":28}],[{\"line\":20,\"ch\":22},{\"line\":20,\"ch\":28}],[{\"line\":21,\"ch\":22},{\"line\":21,\"ch\":28}],[{\"line\":21,\"ch\":0},{\"line\":21,\"ch\":3}],[{\"line\":19,\"ch\":8},{\"line\":19,\"ch\":19}],[{\"line\":20,\"ch\":8},{\"line\":20,\"ch\":19}],[{\"line\":21,\"ch\":8},{\"line\":21,\"ch\":19}],[{\"line\":19,\"ch\":22},{\"line\":19,\"ch\":28}],[{\"line\":20,\"ch\":22},{\"line\":20,\"ch\":28}],[{\"line\":21,\"ch\":22},{\"line\":21,\"ch\":28}],[{\"line\":22,\"ch\":0},{\"line\":22,\"ch\":3}],[{\"line\":23,\"ch\":0},{\"line\":23,\"ch\":3}],[{\"line\":23,\"ch\":8},{\"line\":23,\"ch\":17}],[{\"line\":24,\"ch\":0},{\"line\":24,\"ch\":3}]]"}
!@%STYLE%@!

Inspect the complete example's thread creation, discarded identifiers, thrd_exit usage, and atexit-based cleanup. section 18.6

How Modern C covers it

How deep it goes
Defines an opaque identifier and demonstrates storing and passing it, but does not specify its representation.
Examples
Uses a four-thread game example that stores identifiers in an array, joins threads, and alternatively detaches them.
What it assumes
Assumes familiarity with thrd_create and thread functions while introducing opaque thread identity through concrete examples.
What you can do after
Operate/configure thread lifetimes by storing thrd_t values and supplying them to joining or detaching operations.
How it connects
Connects thrd_t to thrd_create, thrd_join, thrd_current, thrd_equal, thrd_exit, and thrd_detach.

Related concepts

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