Parasol Framework
  • Gallery
  • API
  • Wiki
  • GitHub
    • Audio
    • Core
    • Display
    • Fluid
    • Font
    • Network
    • Vector
    • Audio
    • Sound
    • File
    • MetaClass
    • Module
    • StorageDevice
    • Task
    • Thread
    • Time
    • Compression
    • Config
    • Script
    • XML
    • Controller
    • BlurFX
    • ColourFX
    • CompositeFX
    • ConvolveFX
    • DisplacementFX
    • FilterEffect
    • FloodFX
    • ImageFX
    • LightingFX
    • MergeFX
    • MorphologyFX
    • OffsetFX
    • RemapFX
    • SourceFX
    • TurbulenceFX
    • WaveFunctionFX
    • Scintilla
    • ScintillaSearch
    • Bitmap
    • Clipboard
    • Display
    • Document
    • Font
    • Picture
    • Pointer
    • Surface
    • SVG
    • ClientSocket
    • HTTP
    • NetSocket
    • Proxy
    • Vector
    • VectorClip
    • VectorColour
    • VectorEllipse
    • VectorFilter
    • VectorGradient
    • VectorGroup
    • VectorImage
    • VectorPath
    • VectorPattern
    • VectorPolygon
    • VectorRectangle
    • VectorScene
    • VectorShape
    • VectorSpiral
    • VectorText
    • VectorTransition
    • VectorViewport
    • VectorWave

Thread Class

Threads are created and managed by the Thread class.

The Thread class provides the means to execute and manage threads within an application.

The following code illustrates how to create a temporary thread that is automatically destroyed after the thread_entry() function has completed:

static ERR thread_entry(objThread *Thread) {
   return ERR::Okay;
}

objThread::create thread = { fl::Routine(thread_entry), fl::Flags(THF::AUTO_FREE) };
if (thread.ok()) thread->activate(thread);

To initialise the thread with data, call SetData() prior to execution and read the Data field from within the thread routine.

Structure

The Thread class consists of the following fields:

Access
NameTypeComment
  CallbackFUNCTIONA function reference that will be called when the thread is started.

Set a function reference here to receive a notification when the thread finishes processing. The callback will be executed in the context of the main program loop to minimise resource locking issues.

The prototype for the callback routine is void Callback(objThread *Thread).

  DataAPTRPointer to initialisation data for the thread.

The Data field will point to a data buffer if the SetData() method has previously been called to store data in the thread object. It is paired with the DataSize field, which reflects the size of the data buffer.

  DataSizeINTThe size of the buffer referenced in the Data field.
  ErrorERRReflects the error code returned by the thread routine.
  FlagsTHFOptional flags can be defined here.
NameDescription
THF::AUTO_FREEAutomatically destroy the Thread object when the user routine has completed.
  RoutineFUNCTIONA function reference that will be called when the thread is started.

The routine that will be executed when the thread is activated must be specified here. The function prototype is ERR routine(objThread *Thread).

When the routine is called, a reference to the thread object is passed as a parameter. Once the routine has finished processing, the resulting error code will be stored in the thread object's Error field.

  StackSizeINTThe stack size to allocate for the thread.

The initial size of the thread's stack can be modified by setting this field. It may also be read prior to activation in order to check the default stack size. Changes to the stack size when the thread is active will have no effect.

On some platforms it may not be possible to preset the stack size and the provided value will be ignored.

Actions

The following actions are currently supported:

ActivateSpawn a new thread that calls the function referenced in the Routine field.
DeactivateStops a thread.
ERR acDeactivate(*Object)

Deactivating an active thread will cause it to stop immediately. Stopping a thread in this manner is dangerous and could result in an unstable application.

FreeRemove the object and its resources.
ERR FreeResource(*Object)

Terminating a thread object will destroy the object unless the thread is currently active. If an attempt to free an active thread is made then it will be marked for termination so as to avoid the risk of system corruption.

Methods

The following methods are currently supported:

SetDataAttaches data to the thread.
ERR th::SetData(OBJECTPTR Object, APTR Data, INT Size)
ParameterDescription
DataPointer to the data buffer.
SizeSize of the data buffer. If zero, the pointer is stored directly, with no copy operation taking place.

Use the SetData() method prior to activating a thread so that it can be initialised with user data. The thread will be able to read the data from the Data field.

A copy of the provided data buffer will be stored with the thread object, so there is no need to retain the original data after this method has returned. In some cases it may be desirable to store a direct pointer value and bypass the copy operation. To do this, set the Size parameter to zero.

Error Codes
OkayOperation successful.
ArgsInvalid arguments passed to function.
AllocMemoryA call to AllocMemory() failed to create a new memory block.
NullArgsFunction call missing argument value(s)
Thread class documentation © Paul Manias 1996-2025

THF Type

Thread flags

NameDescription
THF::AUTO_FREEAutomatically destroy the Thread object when the user routine has completed.
Thread module documentation © Paul Manias 1996-2025