Boost logo

Boost Users :

Subject: Re: [Boost-users] [Lambda, Function, etc] Is static dangerous?
From: Viatcheslav.Sysoltsev_at_[hidden]
Date: 2011-05-24 02:46:36


On Mon, 23 May 2011 15:40:06 +0200, Steven Watanabe <watanabesj_at_[hidden]>
wrote:

>
> In C++03, local static anything is dangerous,
> because the initialization is not guaranteed
> to be thread safe. It's safe in C++0x and
> both Boost.Function and Boost.Lambda are
> safe with parallel reads.
>

For Meyers singletons gcc generates thread-safe initialization code:

(gdb) list
1 struct A {
2 A() {}
3 };
4
5 int main()
6 {
7 static A a;
8 return 0;
9 }
(gdb) disas main
Dump of assembler code for function main():
    0x000000000040068c <+0>: push rbp
    0x000000000040068d <+1>: mov rbp,rsp
    0x0000000000400690 <+4>: mov eax,0x601040
    0x0000000000400695 <+9>: movzx eax,BYTE PTR [rax]
    0x0000000000400698 <+12>: test al,al
    0x000000000040069a <+14>: jne 0x4006c3 <main()+55>
    0x000000000040069c <+16>: mov edi,0x601040
    0x00000000004006a1 <+21>: call 0x400560 <__cxa_guard_acquire_at_plt>
    0x00000000004006a6 <+26>: test eax,eax
    0x00000000004006a8 <+28>: setne al
    0x00000000004006ab <+31>: test al,al
    0x00000000004006ad <+33>: je 0x4006c3 <main()+55>
    0x00000000004006af <+35>: mov edi,0x601048
    0x00000000004006b4 <+40>: call 0x4006ca <A::A()>
    0x00000000004006b9 <+45>: mov edi,0x601040
    0x00000000004006be <+50>: call 0x400580 <__cxa_guard_release_at_plt>
    0x00000000004006c3 <+55>: mov eax,0x0
    0x00000000004006c8 <+60>: leave
    0x00000000004006c9 <+61>: ret
End of assembler dump.

>> void f( )
>> {
>> namespace ll = boost::lambda;
>> using boost::function;
>>
>> typedef std::pair<int, int> int_pair;
>>
>> static ll::placeholder1_type x;
>> static function<int(const int_pair &)> second = ll::bind( &
>> int_pair::second, x );
>> }
>>
>

-- Slava


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net