User Tools

Site Tools


thread_set_thread_specific_memory_allocation_.htm
Navigation:  Language Reference > 5 - Declaration Attributes > Variable and Entity Attributes >====== THREAD (set thread-specific memory allocation) ====== Previous pageReturn to chapter overviewNext page

THREAD

blk2blue.jpg

The THREAD attribute declares a variable, FILE, GROUP, QUEUE, or CLASS which is allocated memory separately for each execution thread in the program. This makes the values dependent upon which thread is executing.

A threaded variable must be allocated static memory, so Local data with the THREAD attribute is automatically considered STATIC. This attribute creates runtime “overhead,” particularly on Global or Module data. Therefore, it should be used only when necessary.

In multi-DLL applications, if the THREAD attribute is declared on any of the above valid types, any reference to these types in an associated executable or another DLL must also include the THREAD attribute.

Variable and GROUP Usage

The THREAD attribute declares a static variable which is allocated memory separately for each execution thread in the program. This makes the value contained in the variable dependent upon which thread is executing. Whenever a new execution thread is begun, a new instance of the variable, specific to that thread, is created and initialized to blank or zero (unless the AUTO attribute is also present).

FILE Usage

The THREAD attribute (PROP:THREAD–valid only for a FILE) on a FILE declaration allocates memory for its record buffer, file control block, and other file structure elements separately for each execution thread as each thread is started. This makes the values contained in the record buffer and other file elements dependent upon which thread is executing. Memory for its record buffer,file control block, and other file elements is deallocated when the thread is closed.

Instances of a threaded FILE are considered independent FILEs. Therefore, a FILE must be OPENed and CLOSEd for each new instance.

QUEUE Usage

The THREAD attribute on a QUEUE declaration declares a static QUEUE data buffer which is allocated memory separately for each execution thread in the program. This makes the values contained in the QUEUE dependent upon which thread is executing. Whenever a new execution thread is begun, a new instance of the QUEUE, specific to that thread, is created.

Example:

 PROGRAM

 MAP

Thread1   PROCEDURE

Thread2   PROCEDURE

 END

Names   FILE,DRIVER('Clarion'),PRE(Nam),THREAD  !Threaded file

NbrNdx   INDEX(Nam:Number),OPT

Rec      RECORD

Name      STRING(20)

Number    SHORT

        END

       END

GlobalVar LONG,THREAD     !Each execution thread gets its own copy OF GlobalVar

 CODE

 START(Thread1)

 START(Thread2)

Thread1  PROCEDURE

LocalVar LONG,THREAD      !Local threaded variable (automatically STATIC)

 CODE

 OPEN(Names)             !OPEN creates new record buffer instance

 SET(Names)              !containing the first record in the file

 NEXT(Names)

Thread2   PROCEDURE

SaveQueue QUEUE,THREAD    !Static QUEUE data buffer Thread-specific QUEUE

Name       STRING(20)

Number     SHORT

         END

 CODE

 OPEN(Names)             !OPEN creates another new record buffer instance

 SET(Names)              !containing the last record in the file

 PREVIOUS(Names)

See Also:

START

Data Declarations and Memory Allocation

STATIC

AUTO

Thread Design Considerations

thread_set_thread_specific_memory_allocation_.htm.txt · Last modified: 2021/04/15 15:57 by 127.0.0.1