Last change: 10/02/2002
Author: Luiz Henrique Shigunov
This page describes Thread user interface.
Every program that uses more than one thread must use this interface to create and destroy them.
Programs often need global or static variables that have different values in different threads. Since threads share one memory space, this cannot be achieved with regular variables. Thread-specific data (TSD) is the answer to this need.
Each thread possesses a private memory block, the thread-specific data area, or TSD area for short. This area is indexed by TSD keys. The TSD area associates values of type void * to TSD keys. TSD keys are common to all threads, but the value associated with a given TSD key can be different in each thread.
When a thread is created, its TSD area initially associates NULL with all keys.
int AllocTSD(unsigned int *key, void (*destructor)(void*));
This function allocates a TSD key.
There is a limit on the number of keys available. The value initially associated with the returned key is NULL in all threads.
destructor is still no used.
If all goes ok, key will have the TSD key.
int Create(int (*function)(void*), void *arg, int prop, Thread **id);
This function creates a thread with properties prop.
The thread will start to execute function with argument arg.
prop must be 0 or the sum of:
If all goes ok, id will have the thread id.
int Destroy(Thread *id);
This function destroys thread id.
If id is NULL, current thread is destroyed.
int FreeTSD(unsigned int key);
This function frees the TSD key.
This function gets the thread ID of the caller.
The thread ID
void *GetTSD(unsigned int key);
This function gets the value of the TSD key in the calling thread.
The value of the TSD key.
int SetTSD(unsigned int key, void *value);
This function sets the value of the TSD key to value in the calling thread.