| 1 |
.. title:: clang-tidy - concurrency-mt-unsafe |
| 2 |
|
| 3 |
concurrency-mt-unsafe |
| 4 |
===================== |
| 5 |
|
| 6 |
Checks for some thread-unsafe functions against a black list of |
| 7 |
known-to-be-unsafe functions. Usually they access static variables without |
| 8 |
synchronization (e.g. gmtime(3)) or utilize signals in a racy way. |
| 9 |
The set of functions to check is specified with the `FunctionSet` option. |
| 10 |
|
| 11 |
Note that using some thread-unsafe functions may be still valid in |
| 12 |
concurrent programming if only a single thread is used (e.g. setenv(3)), |
| 13 |
however, some functions may track a state in global variables which |
| 14 |
would be clobbered by subsequent (non-parallel, but concurrent) calls to |
| 15 |
a related function. E.g. the following code suffers from unprotected |
| 16 |
accesses to a global state: |
| 17 |
|
| 18 |
.. code-block:: c++ |
| 19 |
|
| 20 |
// getnetent(3) maintains global state with DB connection, etc. |
| 21 |
// If a concurrent green thread calls getnetent(3), the global state is corrupted. |
| 22 |
netent = getnetent(); |
| 23 |
yield(); |
| 24 |
netent = getnetent(); |
| 25 |
|
| 26 |
|
| 27 |
Examples: |
| 28 |
|
| 29 |
.. code-block:: c++ |
| 30 |
|
| 31 |
tm = gmtime(timep); // uses a global buffer |
| 32 |
|
| 33 |
sleep(1); // implementation may use SIGALRM |
| 34 |
|
| 35 |
.. option:: FunctionSet |
| 36 |
|
| 37 |
Specifies which functions in libc should be considered thread-safe, |
| 38 |
possible values are `posix`, `glibc`, or `any`. |
| 39 |
|
| 40 |
`posix` means POSIX defined thread-unsafe functions. POSIX.1-2001 |
| 41 |
in "2.9.1 Thread-Safety" defines that all functions specified in the |
| 42 |
standard are thread-safe except a predefined list of thread-unsafe |
| 43 |
functions. |
| 44 |
|
| 45 |
Glibc defines some of them as thread-safe (e.g. dirname(3)), but adds |
| 46 |
non-POSIX thread-unsafe ones (e.g. getopt_long(3)). Glibc's list is |
| 47 |
compiled from GNU web documentation with a search for MT-Safe tag: |
| 48 |
https://www.gnu.org/software/libc/manual/html_node/POSIX-Safety-Concepts.html |
| 49 |
|
| 50 |
If you want to identify thread-unsafe API for at least one libc or |
| 51 |
unsure which libc will be used, use `any` (default). |
| 52 |
|