Boost logo

Boost Users :

Subject: Re: [Boost-users] [thread] call_once question
From: Sohail Somani (sohail_at_[hidden])
Date: 2009-03-11 20:14:52


Agustín K-ballo Bergé wrote:
> Is the following code supposed to work (instead of resulting on a stack
> overflow)? If so, what I'm doing wrong? I'm using Boost 1.37.0 on MS
> Visual Studio 2008, Windows.

Do you have a real use case for this type of code? I'm not sure what you
would expect this to do. A reasonable call_once would only set
initialization_flag once call_me_once() actually completes otherwise
call_once is useless.

I don't think once_flag can be used as a local static because the
standard does not guarantee static initialization for non-const local
statics. Or atleast it didn't the last time I looked it up!

BOOST_ONCE_INIT is supposed to be statically initialized. I'm not sure
if that has changed or not in recent releases. I'm pretty sure it
wouldn't change else it would break a lot of code.

> #include <boost/thread/once.hpp>
>
> boost::once_flag initialization_flag = BOOST_ONCE_INIT;
>
> void call_me_once()
> {
> boost::call_once( initialization_flag, call_me_once );
> }
>
> int main( int, char*[] )
> {
> boost::call_once( initialization_flag, call_me_once );
>
> return 0;
> }
>
> Additionaly,
>
> - Can a once_flag be safelly used in a class or local static?
> - Is BOOST_ONCE_INIT guaranteed to statically initialize the flag?
>
> Agustín K-ballo Bergé.-

-- 
Sohail Somani
http://uint32t.blogspot.com

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