Subject: Re: [boost] [threads] Unexpected behaviour of Thread loal storage
From: Andrey Semashev (andrey.semashev_at_[hidden])
Date: 2015-04-29 03:31:48
On Wed, Apr 29, 2015 at 10:07 AM, Fu ji <fujimailing_at_[hidden]> wrote:
> I would like to ask if there is everything ok with my way of thinking.
> I have 3 solutions (projects) in MS Visual Studio.
> Let say:
> SolutionA (static lib)
> SolutionB (linked with Solution A)
> SolutionC (linked with Solution A)
> In Header file of solutionA I have delaration of TLS from boost
> static boost::thread_specific_ptr<int> TLS;
> And now in SolutionB I set tls (TLS.reset(uVariable)) - uVariable is
> integer 2
> Call function from SolutionC and call TLS.get() but there is different
> value than 2.
> But when i return from function to SolutionB and call TLS.get there is
> everything ok (return 2).
> It's ok ? Why in solutionC there is no visibilty of value set in solutionB
> ? It's the same thread (with the same threadId).
Since SolutionA is a static lib, you basically have two different
copies of TLS - one in each of the other two solutions that you link
with SolutionA. On Windows there is no support for symbol relocation
so the two copies continue to be independent after you load SolutionB
and SolutionC binaries. Each of these two solutions work with its own
copy and not the other.
If you want to fix that you have to ensure there is only one instance
of the TLS variable. One way to do that is to make SolutionA a shared
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk