File: threadlocal.yo

package info (click to toggle)
c%2B%2B-annotations 13.02.02-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 13,576 kB
  • sloc: cpp: 25,297; makefile: 1,523; ansic: 165; sh: 126; perl: 90; fortran: 27
file content (36 lines) | stat: -rw-r--r-- 1,850 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
With multi-threaded programs the well-known distinction between global and
local data is somewhat too coarse. For single- and multi-threaded programs
alike, global data are available to all of the program's code, and local data
are available to the function (or compound statement) in which the local data
are defined. But multi-threaded programs may feel the need for an intermediate
type of data, uniquely available to the different threads.

The ti(thread_local) keyword provides this intermediate data level. Global
variables declared as tt(thread_local) are global within each individual
thread. Each thread owns a copy of the tt(thread_local) variables, and may
modify them at will. A tt(thread_local) variable in one thread is completely
separated from that variable in another thread. Here is an example:
        verbinclude(-ans4 examples/threadlocal.cc)
    itemization(
    it() At line 6 the tt(thread_local) variable tt(t_value) is defined. It is
initialized to 100, and that becomes the initial value for each separately
running thread;

    it() In lines 8 through 14 the function tt(modify) is defined. It assigns
a new value to tt(t_value);

    it() At lines 18 and 19 two threads are started, which are immediately
joining the main thread again. 

    it() The main thread itself is also a thread, and it directly calls
tt(modify). 
    )
    Running this program shows that each separate thread starts with
tt(t_value) being 100, and then modifies it without affecting the values of
tt(t_value) used by other threads. 

    Note that, although the tt(t_value) variables are unique to each thread,
identical addresses may be shown for them. Since each thread uses its own
stack, these variables may occupy the same relative locations within their
respective stacks, giving the illusion that their physical addresses are
identical.