Multithreading
suggest changeC11 added a standard threading API in <threads.h>. It includes threads (thrd_t), mutexes (mtx_t), condition variables (cnd_t), one-time initialization (once_flag), and thread-specific storage (tss_t).
The API is optional. An implementation that does not provide it defines __STDC_NO_THREADS__; otherwise, a program can include <threads.h>. Support depends on the C implementation and its standard library, so check the documentation and link options for the target platform.
The central operations are:
thrd_createstarts a thread.thrd_joinwaits for a joinable thread to finish.thrd_detachreleases a thread's resources automatically when it finishes.mtx_lockandmtx_unlockprotect shared state.cnd_waitandcnd_signalcoordinate changes in shared state.
Unsynchronized conflicting accesses to the same object from different threads cause a data race and undefined behavior. Use mutexes or the atomic operations in <stdatomic.h> when objects are shared between threads. volatile is not a synchronization mechanism.