template class EE::System::ThreadLocalPtr

Overview

Pointer to a thread-local variable. More…

#include <threadlocalptr.hpp>

template <typename T>
class ThreadLocalPtr: private EE::System::ThreadLocal {
public:
    // construction

    ThreadLocalPtr(T* value = NULL);

    // methods

    T& operator*() const;
    T* operator->() const;
    operator T*() const;
    ThreadLocalPtr<T>& operator=(T* value);
    ThreadLocalPtr<T>& operator=(const ThreadLocalPtr<T>& right);
};

Inherited Members

public:
    // methods

    void setValue(void* value);
    void* getValue() const;

Detailed Documentation

Pointer to a thread-local variable.

ThreadLocalPtr is a type-safe wrapper for storing pointers to thread-local variables. A thread-local variable holds a different value for each different thread, unlike normal variable that are shared.

Its usage is completely transparent, so that it is similar to manipulating the raw pointer directly (like any smart pointer).

Usage example:

MyClass object1;
MyClass object2;
ThreadLocalPtr<MyClass> objectPtr;

void thread1() {
    objectPtr = &object1; // doesn't impact thread2
    ...
}

void thread2() {
    objectPtr = &object2; // doesn't impact thread1
    ...
}

int main() {
    // Create and launch the two threads
    Thread t1(&thread1);
    Thread t2(&thread2);
    t1.launch();
    t2.launch();

    return 0;
}

Construction

ThreadLocalPtr(T* value = NULL)

Default constructor.

Parameters:

value

Optional value to initalize the variable

Methods

T& operator*() const

Overload of unary operator * Like raw pointers, applying the * operator returns a reference to the pointed object.

Returns:

Reference to the pointed object

T* operator->() const

Overload of operator -> Like raw pointers, applying the -> operator returns the pointed object.

Returns:

Pointed object

operator T*() const

Cast operator to implicitely convert the pointer to its raw pointer type (T*)

Returns:

Pointer to the actual object

ThreadLocalPtr<T>& operator=(T* value)

Assignment operator for a raw pointer parameter.

Parameters:

value

Pointer to assign

Returns:

Reference to self

ThreadLocalPtr<T>& operator=(const ThreadLocalPtr<T>& right)

Assignment operator for a ThreadLocalPtr parameter.

Parameters:

right

ThreadLocalPtr to assign

Returns:

Reference to self