From 623f5a76800cc2e2dd784b8b0bbac0cf50fed767 Mon Sep 17 00:00:00 2001 From: Alexey Milovidov Date: Fri, 24 Dec 2010 19:09:38 +0000 Subject: [PATCH] ThreadNumber: fixed error. --- libs/libcommon/src/ThreadNumber.cpp | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/libs/libcommon/src/ThreadNumber.cpp b/libs/libcommon/src/ThreadNumber.cpp index 05cd4211921..7c535244497 100644 --- a/libs/libcommon/src/ThreadNumber.cpp +++ b/libs/libcommon/src/ThreadNumber.cpp @@ -1,3 +1,6 @@ +#include +#include + #include @@ -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; }