Unit 'exec' Package
[Overview][Constants][Types][Procedures and functions][Variables][Index] [#morphunits]

TLSSetValue

set the task specific value associated with a given Thread-Local Storage index (V51.46)

Declaration

Source position: exec.pas line 2348

function TLSSetValue(

  Idx: LongWord;

  Value: Pointer

):LongInt;

Arguments

Idx

  

index to set value for.

Value

  

value to set.

Function result

Non-zero if the value could be set, zero if out of resources. Note that setting value to nil will always succeed.

Description

Set the task specific value associated with the given Thread-Local Storage index.

Attention

The result of this call must be checked and a viable error handling path taken.

See also

TLSFree

  

Free Thread-Local Storage index

TLSAlloc

  

VarArgs version of TLSAllocA

TLSCallDestructors

  

Call Thread-Local Storage index destructors for the given task (V51.46)

TLSGetValue

  

get the task specific value associated with a given Thread-Local Storage index (V51.46)

Example

type
  TTLSData = record
    foo: LongWord;
    bar: PChar;
    // ... 
  end;
  PTLSData = ^TTtlsdata;

var
  TLSIndex: LongWord; // Set and cleaned up elsewhere
  TLSData: Ptlsdata;

begin
  Data := PTLSData(TLSGetValue(TLSIndex));
  if not Assigned(data) then
  begin
    data := PTLSData(AllocVec(SizeOf(Ttlsdata), MEMF_CLEAR));    
    if not Assigned(data) or (TLSSetValue(TLSIndex, data) <> 0) then
    begin
      if Assigned(data) then
      begin
        FreeVec(data);
        data := NULL;
      end;
    end;
  end;

  if Assigned(data) then
  begin
    // Use the thread specific data. ...
  end;

  //...

  // Thread specific data is no longer needed, free it.
  data := PTLSData(TLSGetValue(tlsindex));
  if Assigned(data) then
  begin
    FreeVec(Data);
    TLSSetValue(tlsindex, nil); // Always succeeds
  end;
end;

Documentation generated on: 2021-07-30