Qore Programming Language Reference Manual: Qore::Thread::AbstractThreadResource Class Reference (original) (raw)

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

#include <[QC_AbstractThreadResource.dox.h](%5Fq%5Fc%5F%5F%5Fabstract%5Fthread%5Fresource%5F8dox%5F8h%5Fsource.html)>

Public Member Methods
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

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:

Example

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();

set_thread_resource(self);

on_error {

cnt.dec();

remove_thread_resource(self);

}

background waiter();

}

stop() {

{

m.lock();

on_exit m.unlock();

exit = True;

cond.signal();

}

cnt.waitForZero();

remove_thread_resource(self);

}

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());

}

}

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

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:

This method is called in the following situations:

Note

See also

Thread Resources