Qore Programming Language Reference Manual  0.8.12.9
Qore::Thread::AbstractThreadResource Class Referenceabstract

This class defines an abstract interface for thread resources. More...

Public Member Functions

abstract nothing cleanup ()
 This method is called by Qore itself when the thread resource is still allocated and requires cleanup. More...
 
 constructor ()
 Creates the internal thread resource object.
 
 destructor ()
 removes the thread resource if set; if so then the cleanup() method is called
 

Detailed Description

This class defines an abstract interface for thread resources.

Overview
Thread resources can be considered properties of a thread that need to be cleaned up when the thread exits or one of the thread cleanup functions is run: The following functions allow classes of this type to be set or removed as thread resources: If the thread resource is still set when the destructor is run, then the destructor frees the thread resource and calls cleanup().
Example
# this class starts a background thread in the constructor that is managed as a thread resource in the creating thread
class ThreadResourceExample inherits AbstractThreadResource {
public {}
private {
Qore::Thread::Mutex m();
Qore::Thread::Condition cond();
Qore::Thread::Counter cnt();
bool exit;
}
constructor() {
start();
}
synchronized private start() {
cnt.inc();
on_error {
cnt.dec();
}
background waiter();
}
stop() {
{
m.lock();
on_exit m.unlock();
exit = True;
cond.signal();
}
cnt.waitForZero();
}
# the cleanup routine stops the thread and throws an exception
cleanup() {
m.lock();
on_exit m.unlock();
if (!exit) {
stop();
throw "THREAD-RESOURCE-ERROR", sprintf("the background thread was stopped during thread resource cleanup; call %s::stop() before exiting the thread to avoid this exception", self.className());
}
}
# the background thread simply waits for the exit condition
private waiter() {
on_exit cnt.dec();
m.lock();
on_exit m.unlock();
while (!exit)
cond.wait(m);
}
}
See also
Thread Resources
Since
Qore 0.8.12

Member Function Documentation

◆ cleanup()

abstract nothing Qore::Thread::AbstractThreadResource::cleanup ( )
pure virtual

This method is called by Qore itself when the thread resource is still allocated and requires cleanup.

This method should clean up the resource and normally should throw an appropriate exception explaining:

  • what the programming error was (ex: failing to unlock a lock before the thread exited)
  • what happened (ex: the lock was automatically released)
  • how to prevent it from happening in the future (ex: make sure and release the lock before exiting the thread)

This method is called in the following situations:

Note
See also
Thread Resources