This diagnostic rule is based on the MISRA (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:
thrd_t: thrd_create, thrd_detach, thrd_join, thrd_equal, thrd_current, thrd_sleep, thrd_yield, thrd_exit;mtx_t: mtx_init, mtx_lock, mtx_trylock, mtx_timedlock, mtx_unlock, mtx_destroy;cnd_t: cnd_init, cnd_signal, cnd_broadcast, cnd_wait, cnd_timedwait, cnd_destroy;tss_t: tss_create, tss_get, tss_set, tss_delete.Directly accessing struct members, as well as copying, modifying, or comparing them in any other way, leads to undefined behavior.
Look at the example:
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:
int threads_equal(const thrd_t *t1, const thrd_t *t2)
{
return thrd_equal(t1, t2);
}
This diagnostic rule is classified as:
|
Was this page helpful?