| **Navigation:**  [[introduction.htm|Language Reference]] > 5 - Declaration Attributes > Variable and Entity Attributes >====== THREAD (set thread-specific memory allocation) ====== | [[static set allocate static memory .htm|{{btn_prev_n.gif|Previous page}}]][[introduction.htm|{{btn_home_n.gif|Return to chapter overview}}]][[type type definition .htm|{{btn_next_n.gif|Next page}}]] | | || **THREAD** {{blk2blue.jpg|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 return new execution thread .htm|START]] [[data declarations and memory allocation.htm|Data Declarations and Memory Allocation]] [[static set allocate static memory .htm|STATIC]] [[auto uninitialized local variable .htm|AUTO]] [[mtcode differences between clarion 5 5 and clarion 6.htm|Thread Design Considerations]]