Click here to Skip to main content
15,891,903 members
Articles / Programming Languages / C++

Thread Local Storage - The C++ Way

Rate me:
Please Sign up or sign in to vote.
4.69/5 (23 votes)
27 Aug 20048 min read 185.1K   3.2K   61  
How to put class objects in TLS with automatic destructor invocation.
#ifndef TLS_H_
#define TLS_H_

# pragma warning(disable:4284) // odd return type for operator->

#ifdef TLS_ALLOC
#	define basic_default basic_tls_alloc
#else
#	define basic_default basic_tls_declspec
#endif

class basic_tls_declspec {
public:

	basic_tls_declspec(void (*pvpv)(void*));
	static void thread_term(void);

protected:

	void set(void* pv);     
	void* get() const;

private:

	const unsigned id;

	static void** thread_init(void);
	void (*pdtor)(void*);

	basic_tls_declspec* const prev;
	static basic_tls_declspec*  last;
	static unsigned idmax;
};

class basic_tls_alloc {
public:

	basic_tls_alloc(void (*pvpv)(void*));
	static void thread_term(void);

protected:

	void set(void* pv);     
	void* get() const;

private:

	const unsigned id;

	static void** thread_init(void);
	void (*pdtor)(void*);

	basic_tls_alloc* const prev;
	static basic_tls_alloc*  last;
	static unsigned idmax;
};

template<class T, class B = basic_default> class tls_ptr : private B {
public:

	tls_ptr(void (*cleanup)(void*) = &dtor) : B(cleanup) {}
    inline T* get() const {	return static_cast<T*>(B::get()); }
    inline T* operator->() const { return get(); }
    inline T& operator*() const { return *get(); }
	T* release() { T* temp = get(); if(temp) B::set(0); return temp; }
    void reset(T* p=0)
    {
        T* cur = get();
        if (cur == p) return;
		B::set(p);
        if (cur) dtor(cur);
    }

private:
	static void dtor(void* p) { delete static_cast<T*>(p);	}
};

// autolink section
#ifndef TLS_BUILD_LIB

#if (_MSC_VER == 1200)
#	define TOOLSET "vc6"
#elif (_MSC_VER == 1300)
#	define TOOLSET "vc7"
#elif (_MSC_VER >= 1310)
#	define TOOLSET "vc71"
#endif

#ifdef _DEBUG
#	ifdef _DLL
#		define RTOPT "-d"
#	else
#		define RTOPT "-sd"
#	endif
#else
#	ifdef _DLL
#		define RTOPT
#	else
#		define RTOPT "-s"
#	endif
#endif

#pragma comment(lib, "libtls" "_" TOOLSET "-mt" RTOPT ".lib")
#ifdef _DEBUG
#	pragma message("Linking with: " "libtls" "_" TOOLSET "-mt" RTOPT ".lib")
#endif
#endif

#undef TOOLSET
#undef RTOPT

#endif

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Austria Austria
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions