Detach the thread from within itself to avoid race condition

Fixes: #188
This commit is contained in:
Mike Gelfand 2017-02-23 00:07:39 +03:00
parent d5d9b61212
commit 0f75e64d71
1 changed files with 5 additions and 1 deletions

View File

@ -98,11 +98,16 @@ tr_amInThread (const tr_thread * t)
static ThreadFuncReturnType
ThreadFunc (void * _t)
{
#ifndef _WIN32
pthread_detach (pthread_self ());
#endif
tr_thread * t = _t;
t->func (t->arg);
tr_free (t);
#ifdef _WIN32
_endthreadex (0);
return 0;
@ -125,7 +130,6 @@ tr_threadNew (void (*func)(void *), void * arg)
}
#else
pthread_create (&t->thread, NULL, (void* (*)(void*))ThreadFunc, t);
pthread_detach (t->thread);
#endif
return t;