[thread][thread_specific_ptr]

View: New views
3 Messages — Rating Filter:   Alert me  

[thread][thread_specific_ptr]

by Sid Sacek-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

In VC++ and GCC a user creates global TLS like this:

VC++
      __declspec( thread ) int  tls_1;

GCC
      __thread int tls_2;


But using the boost library

      boost::thread_specific_ptr< int > tls_3;


doesn't provide an initialized global variable as one would expect from thread-local-storage. Using tls_3 crashes the program if it's not pre-initialized to some object. The implication is that every time a new thread is created, it needs to first instantiate all global TLS objects.

Am I not understanding the intentions of this class?  Is there a proper or intended way of using this class? I couldn't find any example code in the boost archives.

Thanks,
-Sid

_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost

Re: [thread][thread_specific_ptr]

by Steven Watanabe-4 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

AMDG

Sid Sacek wrote:

> In VC++ and GCC a user creates global TLS like this:
>
> VC++
>       __declspec( thread ) int  tls_1;
>
> GCC
>       __thread int tls_2;
>
>
> But using the boost library
>
>       boost::thread_specific_ptr< int > tls_3;
>
>
> doesn't provide an initialized global variable as one would expect from thread-local-storage. Using tls_3 crashes the program if it's not pre-initialized to some object. The implication is that every time a new thread is created, it needs to first instantiate all global TLS objects.
>
> Am I not understanding the intentions of this class?  Is there a proper or intended way of using this class? I couldn't find any example code in the boost archives.
>  

boost::thread_specific_ptr acts as a pointer rather than a value.
It will start out as a null pointer in each thread.  You can test
whether it has been set to something else with

if(tls_3.get() != 0)

In Christ,
Steven Watanabe

_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost

Re: [thread][thread_specific_ptr]

by Sid Sacek-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

> boost::thread_specific_ptr acts as a pointer rather than a value.
> It will start out as a null pointer in each thread.  You can test
> whether it has been set to something else with
>
> if(tls_3.get() != 0)
>
> In Christ,
> Steven Watanabe


I see. I have to give this class and its limitations some more thought.

For the time being, I see myself having to do that test before each usage of the globals.
ie.

    if ( tls_3.get() == NULL )  tls_3.reset( new int );
    *tls_3 = 999;

This kind of coding can get expensive if used constantly in that manner.
-Sid Sacek

_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost