Xfce Foundation Classes
Main Page | Index | Namespace List | Alphabetical List | Class List | File List

# Xfc::G::Private< T > Class Template Reference

A run-time GStaticPrivate C++ wrapper interface. More...

`#include <xfc/glib/thread.hh>`

Inheritance diagram for Xfc::G::Private< T >:

List of all members.

## Public Member Functions

Constructors

- Private ()
    Constructs and initializes a new private key.
- ~Private ()
    Destructor. Frees the private key.

## Detailed Description

### template<typename T>
class Xfc::G::Private< T >

A run-time GStaticPrivate C++ wrapper interface.

The Private object represents a thread private data key. Threads can thereby obtain and set a pointer, which is private to the current thread. Take our give_me_next_number() example from previously. Now we don't want current_number to be shared between the threads, but to be private to each thread. This can be done as follows:

Example: Using G::Private for per-thread data.

        G::Private<int> *current_number_key = 0; // Must be initialized somewhere.

        int give_me_next_number()
        {
            int *current_number = current_number_key->get();

            if (!current_number)
            {
                    current_number = new int;
                    *current_number = 0;
                    current_number_key->set(current_number);
            }
            *current_number = calc_next_number(*current_number);
            return *current_number;
        }

Here the pointer belonging to the key current_number_key is read. If it is null, it has not been set yet. Then get memory for an integer value, assign this memory to the pointer and write the pointer back. Now we have an integer value, that is private to the current thread.

The documentation for this class was generated from the following file:

- thread.hh

Xfce Foundation Classes

Copyright © 2004-2005 The XFC Development Team | XFC 4.3