The following two variables need to have a unique value for each thread in which they are defined:
The C library maintains thread-specific values for these global variables.
Other variables, such as _environ, are common across all threads and do not automatically get unique values in each thread that uses them.
For example, the following program shows how the value of errno is unique to each thread. Although an error occurs in the thread openProc, the value of errno is 0 because it is checked from the main thread.
Example of a Per-Thread Variable
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
int done = 0;
void _Optlink openProc(void * argument)
{
FILE *filePointer ;
filePointer = fopen("C:\\OS2","w");
printf("openProc, errno = %d\n",errno);
done = 1;
}
int main(void)
{
char holder[80];
errno = 0 ;
_beginthread(openProc,NULL,4096,NULL) ;
while (1) /* Break only when the thread is done. */
{
printf("Press <enter> to continue.\n");
gets(holder);
if (done)
break ;
printf("The thread is still executing! \n");
}
printf("Main program, errno = %d.\n",errno);
return 0;
/* The expected output is:
Press <enter> to continue.
openProc, errno = 60
Main program, errno = 0. */
}
|
When you call longjmp, the buffer you pass to it must have been initialized by a call to setjmp on the same thread. If the buffer was not initialized on the same thread, the process terminates.
The internal buffers used by asctime, ctime, gmtime, and localtime are also allocated on a per-thread basis. That is, these functions return addresses of buffers that are specific to the thread from where the function was called.
There is one seed per thread for generating random numbers with the rand and srand functions. This characteristic ensures that the pseudorandom numbers generated in each thread are independent of other threads. Each thread starts with the same seed (1); that is, each thread gets the same sequence of pseudorandom numbers unless the seed is changed by a call to srand.
Global Variables Requiring Serialization
These global variables containing environment strings should be
treated as read-only data. They should be modified only by
library functions:
int _daylight; long _timezone; char *_tzname[2]; char _osmajor; char _osminor; char _osmode; char **_environ;
Note: The _timezone variable contains the time difference (in seconds) between the local time and Greenwich Mean Time (GMT).
The environment strings are copied from the OS/2 environment when a program starts. This procedure is the same in multithread and single-thread programs. Because all threads share the environment strings, any change made to the strings by one thread affects the environment accessed by the other threads.
Each thread can call getenv to obtain a copy of the environment strings and copy the string to a private data buffer so that any later changes made to the environment by putenv will not affect it. If the thread must always access the latest version of the environment strings, it must call getenv each time.