Boost logo

Boost :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2003-02-19 10:13:22


Ken Hagan wrote:
> Peter Dimov wrote:
>>
>> &k does not exist yet at compile-time (in a pointer to int form),
>> when templates are instantiated.
>
> It doesn't have to. We're instantiating a template, not calling a
> function, so if "&k" has the type "pointer to thread-local int" then
> the compiler knows that and can instantiate the appropriate code.

Indeed it can, and it (MSVC 7.1b) even does. I stand corrected.

#include <iostream>
#include <typeinfo>
#include <windows.h>
#include <process.h>
#include <boost/detail/lightweight_mutex.hpp>

typedef boost::detail::lightweight_mutex mutex_type;
mutex_type m;

template<uintptr_t * p> struct C
{
    void f()
    {
        std::cout << " p: " << p << ": " << *p << std::endl;
    }
};

__declspec(thread) uintptr_t k = 0;

unsigned __stdcall f(void * pt)
{
    k = *(uintptr_t *)pt;
    mutex_type::scoped_lock lock(m);
    C<&k> ck;
    ck.f();
    std::cout << "&k: " << &k << ": " << k << std::endl;
    return 0;
}

int main()
{
    uintptr_t t1 = _beginthreadex(0, 0, f, &t1, 0, 0);
    uintptr_t t2 = _beginthreadex(0, 0, f, &t2, 0, 0);
    uintptr_t t3 = _beginthreadex(0, 0, f, &t3, 0, 0);
    ::Sleep(250);
    ::CloseHandle((HANDLE)t1);
    ::CloseHandle((HANDLE)t2);
    ::CloseHandle((HANDLE)t3);
}


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk