From 4b2f9e78fcaf07fc81033c31b0127857b10efc07 Mon Sep 17 00:00:00 2001 From: Vadim Pisarevsky Date: Tue, 26 Apr 2011 12:20:42 +0000 Subject: [PATCH] fixed repeated allocation of RNG on each theRNG() call (thanks to barjenbr for the patch) --- modules/core/src/rand.cpp | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/modules/core/src/rand.cpp b/modules/core/src/rand.cpp index f0701e82e5..d10e1377eb 100644 --- a/modules/core/src/rand.cpp +++ b/modules/core/src/rand.cpp @@ -689,19 +689,22 @@ RNG& theRNG() #else static pthread_key_t tlsRNGKey = 0; +static pthread_once_t tlsRNGKeyOnce = PTHREAD_ONCE_INIT; static void deleteRNG(void* data) { delete (RNG*)data; } +static void makeRNGKey() +{ + int errcode = pthread_key_create(&tlsRNGKey, deleteRNG); + CV_Assert(errcode == 0); +} + RNG& theRNG() { - if( !tlsRNGKey ) - { - int errcode = pthread_key_create(&tlsRNGKey, deleteRNG); - CV_Assert(errcode == 0); - } + pthread_once(&tlsRNGKeyOnce, makeRNGKey); RNG* rng = (RNG*)pthread_getspecific(tlsRNGKey); if( !rng ) {