[Lambda, Function, etc] Is static dangerous?

Hi All Is it dangerous to write static Boost.Functions or Boost.Lamda placeholders in a multi-theaded context? I think that might be a really stoopid quesion - I'm just checking! Thx - Rob. eg, #include <boost/function.hpp> #include <boost/lambda/bind.hpp> 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 ); }

AMDG On 05/23/2011 05:52 AM, Robert Jones wrote:
Hi All
Is it dangerous to write static Boost.Functions or Boost.Lamda placeholders in a multi-theaded context? I think that might be a really stoopid quesion - I'm just checking!
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.
eg,
#include <boost/function.hpp> #include <boost/lambda/bind.hpp>
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 ); }
In Christ, Steven Watanabe

On Mon, 23 May 2011 15:40:06 +0200, Steven Watanabe <watanabesj@gmail.com> 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@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@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
participants (3)
-
Robert Jones
-
Steven Watanabe
-
Viatcheslav.Sysoltsev@h-d-gmbh.de