﻿# V2670\. MISRA\. Thread objects, thread synchronization objects, and thread\-specific storage pointers should only be accessed by the appropriate Standard Library functions\.

This diagnostic rule is based on the [MISRA](https://misra.org.uk/) \(Motor Industry Software Reliability Association\) software development guidelines\.

This diagnostic rule is relevant only for C\.

Thread objects \(`thrd_t`\), mutexes \(`mtx_t`\), condition variables \(`cnd_t`\), and thread\-specific storages \(`tss_t`\) should be accessed exclusively via special functions provided by the standard library:

* for `thrd_t`: [`thrd_create`](https://en.cppreference.com/w/c/thread/thrd_create.html), [`thrd_detach`](https://en.cppreference.com/w/c/thread/thrd_detach.html), [`thrd_join`](https://en.cppreference.com/w/c/thread/thrd_join.html), [`thrd_equal`](https://en.cppreference.com/w/c/thread/thrd_equal.html), [`thrd_current`](https://en.cppreference.com/w/c/thread/thrd_current.html), [`thrd_sleep`](https://en.cppreference.com/w/c/thread/thrd_sleep.html), [`thrd_yield`](https://en.cppreference.com/w/c/thread/thrd_yield.html), [`thrd_exit`](https://en.cppreference.com/w/c/thread/thrd_exit.html);
* for `mtx_t`: [`mtx_init`](https://en.cppreference.com/w/c/thread/mtx_init.html), [`mtx_lock`](https://en.cppreference.com/w/c/thread/mtx_lock.html), [`mtx_trylock`](https://en.cppreference.com/w/c/thread/mtx_trylock.html), [`mtx_timedlock`](https://en.cppreference.com/w/c/thread/mtx_timedlock.html), [`mtx_unlock`](https://en.cppreference.com/w/c/thread/mtx_unlock.html), [`mtx_destroy`](https://en.cppreference.com/w/c/thread/mtx_destroy.html);
* for `cnd_t`: [`cnd_init`](https://en.cppreference.com/w/c/thread/cnd_init.html), [`cnd_signal`](https://en.cppreference.com/w/c/thread/cnd_signal.html), [`cnd_broadcast`](https://en.cppreference.com/w/c/thread/cnd_broadcast.html), [`cnd_wait`](https://en.cppreference.com/w/c/thread/cnd_wait.html), [`cnd_timedwait`](https://en.cppreference.com/w/c/thread/cnd_timedwait.html), [`cnd_destroy`](https://en.cppreference.com/w/c/thread/cnd_destroy.html);
* for `tss_t`: [`tss_create`](https://en.cppreference.com/w/c/thread/tss_create.html), [`tss_get`](https://en.cppreference.com/w/c/thread/tss_get.html), [`tss_set`](https://en.cppreference.com/w/c/thread/tss_set.html), [`tss_delete`](https://en.cppreference.com/w/c/thread/tss_delete.html)\.

Directly accessing struct members, as well as copying, modifying, or comparing them in any other way, leads to undefined behavior\.

Look at the example:

```cpp
thrd_t thread1, thread2;

int threads_equal(const thrd_t *t1, const thrd_t *t2)
{
  return memcmp(t1, t2, sizeof(thrd_t));
}
```

The `memcmp` function compares two `thrd_t` objects byte by byte\. However, a developer may not know exactly how the control flow will be implemented in a particular standard C library\. This can result in two identical threads being treated as different, for example, due to alignment bytes\.

To properly compare objects of the `thrd_t` type and write portable code, use the `thrd_equal` function:

```cpp
int threads_equal(const thrd_t *t1, const thrd_t *t2)
{
  return thrd_equal(t1, t2);
}
```