From WikiChip
Concurrency - C
< c
Revision as of 06:30, 4 January 2015 by Inject (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

This article is about C11 thread support, not POSIX Threads.

The C programming language introduced the semantics necessary for writing a multithreaded program starting with the C11 standard. The standard also specified a set of functions to facilitate inter‐thread communication. These features are provided via the thread library headers which can be utilized by including the <threads.h> and <stdatomic.h> standard headers.

Optional

Thread support is an optional features for implementations where support for multithreaded programs is possible. The macro __STDC_NO_THREADS__ must be set to 1 if the implementation does not support the <threads.h> header.

#if (__STDC_VERSION__ >= 201112L) && (__STDC_NO_THREADS__ != 1)
    /* thread support */
#endif

Creating A Thread

One of the functions included in <threads.h> is thrd_create(),

int thrd_create(thrd_t *thr, thrd_start_t func, void *arg);

Which creates a new thread executing func(arg). On a successful operation, the object pointed to by thr to the identifier of the newly created thread and thrd_success will be returned. Upon failure thrd_error or thrd_nomem will be returned indicating insufficient amount of memory or failure to honor the request.