Re: Confused about a thread-safe singleton example.
On Dec 5, 5:40 am, "Chris M. Thomasson" <n...@spam.invalid> wrote:
"James Kanze" <james.ka...@gmail.com> wrote in message
news:ff9ba37a-3063-4d8c-b848-25dfbc992f5b@w1g2000prm.googlegroups.com...
On Dec 4, 5:45 am, "Chris M. Thomasson" <n...@spam.invalid>
wrote:
<jason.cipri...@gmail.com> wrote in message
news:962e1281-0dd3-4571-b941-1de092ff63ed@j32g2000yqn.googlegroups.com=
....
You can "easily" use `pthread_once()' and compiler
specific directives to construct a fairly "efficient"
singleton pattern;
[...]
I'll admit that I don't see what all the uproar is about.
It's relatively simple to ensure that initialization occurs
before main, so you don't need any locking. And even if you
do, grabbing an uncontended mutex lock just isn't that
expensive.
The overhead associated with uncontended locks for frequently
accessed singleton data-structures can be quite significant
indeed. For instance, there can be a #StoreLoad memory order
constraint _and_ an atomic RMW instruction attached to every
lock acquisition. Also, there can be a #LoadStore memory order
constraint and atomic RMW along side every lock release.
Have you actually measured it? I was pleasantly surprised when
I did (on a Sun Sparc, under Solaris). It's true that you need
the fences, and that they do increase execution time, but
they're unavoidable in any working solution anyway. (The code
you posted is NOT thread safe, and will not work on any number
of architectures, including Sparc, Alpha, Itanium...)
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34