/* This function will detach a thread. The thread object will be removed from thread queue and detached/deleted from system object management. */
rt_err_t rt_thread_detach(rt_thread_t thread)
/* This function will detach a thread. The thread object will be removed from thread queue and detached/deleted from system object management. */
rt_err_t rt_thread_detach(rt_thread_t thread)
{
    rt_base_t lock;
    RT_ASSERT(thread != RT_NULL);
    rt_schedule_remove_thread(thread);
    rt_timer_detach(&(thread->thread_timer));
    thread->stat = RT_THREAD_CLOSE;
    rt_object_detach((rt_object_t)thread);
    if (thread->cleanup != RT_NULL)
    {
        lock = rt_hw_interrupt_disable();
        rt_list_insert_after(&rt_thread_defunct, &(thread->tlist));
        rt_hw_interrupt_enable(lock);
    }
    return RT_EOK;
}