From WikiChip
Difference between revisions of "c/concurrency"
< c

(fixed/rewrote grammar)
m
 
(3 intermediate revisions by 2 users not shown)
Line 1: Line 1:
<small>''This article is about C11 thread support, not [[POSIX Threads]].''</small>
+
{{c title|Concurrency}}
 +
<small>''This article is about [[C11]] thread support, not [[POSIX Threads]].''</small>
  
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 - C|<threads.h>]] and [[stdatomic.h - C|<stdatomic.h>]] [[C standard library|standard headers]].
+
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 {{C|threads.h|<threads.h>}} and {{C|stdatomic.h|<stdatomic.h>}} {{C|standard library|standard headers}}.
  
 
== Optional ==
 
== Optional ==
Thread support is an optional features for implementations where support for multithreaded programs is possible. The macro <code>__STDC_NO_THREADS__</code> must be set to <code>1</code> if the implementation does not support the [[threads.h - C|<threads.h>]] header.
+
Thread support is an optional features for implementations where support for multithreaded programs is possible. The macro <code>__STDC_NO_THREADS__</code> must be set to <code>1</code> if the implementation does not support the {{C|threads.h|<threads.h>}} header.
  
 
<source lang="C">
 
<source lang="C">
Line 13: Line 14:
  
 
== Creating A Thread ==
 
== Creating A Thread ==
One of the functions included in [[threads.h - C|<threads.h>]] is [[threads.h/thrd_create - C|thrd_create()]],
+
One of the functions included in {{C|threads.h|<threads.h>}} is {{C|threads.h/thrd_create|thrd_create()}},
  
 
<code lang="C>int thrd_create(thrd_t *thr, thrd_start_t func, void *arg);</code>
 
<code lang="C>int thrd_create(thrd_t *thr, thrd_start_t func, void *arg);</code>

Latest revision as of 07:30, 4 January 2015

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[edit]

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[edit]

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.