6a7 Subj : Re: Can C++ local static objects be made thread safe? To : comp.programming.threads From : gottlobfrege Date : Wed Feb 16 2005 09:59 pm Alexander Terekhov wrote: > gottlobfrege@gmail.com wrote: > [...] > > operations, which, I hope is slightly better than boost's version, > > which uses a global named mutex...) > > What's your problem with global named mutexes created lazily > on once()'s slow path? Note that are destructed pretty soon. > > regards, > alexander. No big problem, but a critical section is still faster (isn't it?). I know one project where they thought they might need hundreds of once-guarded objects, so it might make a difference. (Obviously they may have had other design issues, but they were trying to work with existing code.) Also, my original reason for looking at once wasn't for performance (I didn't even know about boost once or pthread_once), it was syntax. I wanted (and made) this syntax: int foo() { static Once once; if (Once::Sentry sentry(once)) { // do some stuff once } // no thread gets here until the once stuff is done. // (and only done once, of course.) } (what I really wanted was a 'once' keyword: once { ... } but the Once and Once::Sentry were close enough :-) By the way, what do people think should happen in the case of recursion - ie calling foo() inside the Once block? (Or similar recursion with pthread_once?) I could throw an assert, or just not worry about it (I suspect my current implementation lets them back into the once block and recurses. Alternatively I could deadlock, but that seems even worse...) Thanks, Tony . 0