ThreadNumber: fixed error.

This commit is contained in:
Alexey Milovidov 2010-12-24 19:09:38 +00:00
parent e673533a0b
commit 623f5a7680

View File

@ -1,3 +1,6 @@
#include <pthread.h>
#include <Yandex/optimization.h>
#include <Yandex/ThreadNumber.h>
@ -6,7 +9,13 @@ unsigned threads = 0;
unsigned ThreadNumber::get()
{
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
if (unlikely(!thread_number))
thread_number = __sync_add_and_fetch(&threads, 1);
{
pthread_mutex_lock(&mutex);
if (likely(!thread_number))
thread_number = ++threads;
pthread_mutex_unlock(&mutex);
}
return thread_number;
}