[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
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
#include void f( ) { namespace ll = boost::lambda; using boost::function;
typedef std::pair
int_pair; static ll::placeholder1_type x; static function
second = ll::bind( & int_pair::second, x ); }
In Christ, Steven Watanabe
On Mon, 23 May 2011 15:40:06 +0200, Steven Watanabe
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
void f( ) { namespace ll = boost::lambda; using boost::function;
typedef std::pair
int_pair; static ll::placeholder1_type x; static function
second = ll::bind( & int_pair::second, x ); }
-- Slava
participants (3)
-
Robert Jones
-
Steven Watanabe
-
Viatcheslav.Sysoltsev@h-d-gmbh.de