MCPcopy Create free account
hub / github.com/RT-Thread/rt-thread / pthread_detach

Function pthread_detach

components/libc/posix/pthreads/pthread.c:533–574  ·  view source on GitHub ↗

* @brief Marks a thread as detached, allowing its resources to be automatically released upon termination. * * The `pthread_detach` function separates the specified thread from the calling thread. Once a thread is detached, * its resources will be automatically reclaimed by the system upon the thread's termination. A detached thread cannot * be joined using `pthread_join`. * * @param[in] thr

Source from the content-addressed store, hash-verified

531 * @see pthread_create, pthread_join, pthread_attr_setdetachstate
532 */
533int pthread_detach(pthread_t thread)
534{
535 int ret = 0;
536 _pthread_data_t *ptd = _pthread_get_data(thread);
537 if (ptd == RT_NULL)
538 {
539 /* invalid pthread id */
540 ret = EINVAL;
541 goto __exit;
542 }
543
544 if (ptd->attr.detachstate == PTHREAD_CREATE_DETACHED)
545 {
546 /* The implementation has detected that the value specified by thread does not refer
547 * to a joinable thread.
548 */
549 ret = EINVAL;
550 goto __exit;
551 }
552
553 if ((RT_SCHED_CTX(ptd->tid).stat & RT_THREAD_STAT_MASK) == RT_THREAD_CLOSE)
554 {
555 /* destroy this pthread */
556 _pthread_data_destroy(ptd);
557 goto __exit;
558 }
559 else
560 {
561 /* change to detach state */
562 ptd->attr.detachstate = PTHREAD_CREATE_DETACHED;
563
564 /* detach joinable semaphore */
565 if (ptd->joinable_sem)
566 {
567 rt_sem_delete(ptd->joinable_sem);
568 ptd->joinable_sem = RT_NULL;
569 }
570 }
571
572__exit:
573 return ret;
574}
575RTM_EXPORT(pthread_detach);
576
577/**

Callers 2

__ARM_TPL_thread_detachFunction · 0.85
detachMethod · 0.85

Calls 3

_pthread_get_dataFunction · 0.85
_pthread_data_destroyFunction · 0.85
rt_sem_deleteFunction · 0.85

Tested by

no test coverage detected