Qore Programming Language Reference Manual  0.8.11.1
Threading Functions

Functions

nothing Qore::delete_all_thread_data ()
 Deletes all keys in the thread-local data hash. More...
 
nothing Qore::delete_thread_data (...)
 Deletes the data associated to one or more keys in the thread-local data hash; if the data is an object, then it is destroyed. More...
 
nothing Qore::delete_thread_data (list l)
 Deletes the data associated to one or more keys in the thread-local data hash; if the data is an object, then it is destroyed. More...
 
hash Qore::getAllThreadCallStacks ()
 Returns a hash of call stacks keyed by each TID (thread ID) More...
 
hash Qore::get_all_thread_data ()
 Returns the entire thread-local data hash. More...
 
any Qore::get_thread_data (string key)
 Returns the value of the thread-local data attached to the key passed. More...
 
nothing Qore::get_thread_data ()
 This function variant does nothing at all; it is only included for backwards-compatibility with qore prior to version 0.8.0 for functions that would ignore type errors in arguments. More...
 
*TimeZone Qore::get_thread_tz ()
 Returns any TimeZone set for the current thread, NOTHING if none is set. More...
 
int Qore::gettid ()
 Returns the Qore thread ID (TID) of the current thread. More...
 
nothing Qore::mark_thread_resources ()
 Marks thread resources so that any thread resources left allocated after this call will be cleaned up when throw_thread_resource_exceptions_to_mark() is called. More...
 
int Qore::num_threads ()
 Returns the current number of threads in the process (not including the special signal handling thread) More...
 
*hash Qore::remove_thread_data (...)
 Removes the data associated to one or more keys in the thread-local data hash and returns the data removed. More...
 
hash Qore::remove_thread_data (list l)
 Removes the data associated to one or more keys in the thread-local data hash from a literal list passed as the first argument and returns the data removed. More...
 
nothing Qore::save_thread_data (hash h)
 Saves the data passed in the thread-local hash; all keys are merged into the thread-local hash, overwriting any information that may have been there before. More...
 
nothing Qore::save_thread_data (string key, any value)
 Saves the data passed against the key passed as an argument in thread-local storage. More...
 
nothing Qore::save_thread_data ()
 This function variant does nothing at all; it is only included for backwards-compatibility with qore prior to version 0.8.0 for functions that would ignore type errors in arguments. More...
 
bool Qore::set_thread_init (code init)
 Sets a call reference or closure to run every time a new thread is started. More...
 
nothing Qore::set_thread_tz (TimeZone zone)
 Sets the default time zone for the current thread. More...
 
nothing Qore::set_thread_tz ()
 Clears the thread-local time zone for the current thread; after this call TimeZone::get() will return the value set for the current Program. More...
 
list Qore::thread_list ()
 Returns a list of all current thread IDs. More...
 
nothing Qore::throwThreadResourceExceptions ()
 Immediately runs all thread resource cleanup routines for the current thread and throws all associated exceptions. More...
 
bool Qore::throw_thread_resource_exceptions_to_mark ()
 Immediately runs all thread resource cleanup routines for the current thread for thread resources created since the last call to mark_thread_resources() and throws all associated exceptions. More...
 

Detailed Description

Threading functions

Function Documentation

nothing Qore::delete_all_thread_data ( )

Deletes all keys in the thread-local data hash.

Restrictions:
Qore::PO_NO_THREAD_CONTROL
Example:
Note
This function does not throw any exceptions, however if an object is deleted from the thread-local data hash, then it could throw an exception in its destructor
nothing Qore::delete_thread_data (   ...)

Deletes the data associated to one or more keys in the thread-local data hash; if the data is an object, then it is destroyed.

Restrictions:
Qore::PO_NO_THREAD_CONTROL
Parameters
...Deletes the data associated to one or more keys in the thread-local data hash corresponding to each string argument in the top-level argument list; arguments are converted to strings if necessary
Example:
delete_thread_data("key1", "key2");
Note
This function does not throw any exceptions, however if an object is deleted from the thread-local data hash, then it could throw an exception in its destructor
See also
remove_thread_data() for a similar function that does not explicitly destroy objects in the thread-local data hash
nothing Qore::delete_thread_data ( list  l)

Deletes the data associated to one or more keys in the thread-local data hash; if the data is an object, then it is destroyed.

Restrictions:
Qore::PO_NO_THREAD_CONTROL
Parameters
la list of key names for deleting keys in the thread-local data hash; values are converted to strings if necessary
Example:
delete_thread_data($list_of_keys);
Note
This function does not throw any exceptions, however if an object is deleted from the thread-local data hash, then it could throw an exception in its destructor
See also
remove_thread_data() for a similar function that does not explicitly destroy objects in the thread-local data hash
hash Qore::get_all_thread_data ( )

Returns the entire thread-local data hash.

Returns
the entire thread-local data hash
Restrictions:
Qore::PO_NO_THREAD_CONTROL, Qore::PO_NO_THREAD_INFO
Code Flags:
CONSTANT
Example:
any Qore::get_thread_data ( string  key)

Returns the value of the thread-local data attached to the key passed.

Returns
the value of the thread-local data attached to the key passed
Restrictions:
Qore::PO_NO_THREAD_CONTROL, Qore::PO_NO_THREAD_INFO
Code Flags:
CONSTANT
Example:
my any $data = get_thread_data("key1");
nothing Qore::get_thread_data ( )

This function variant does nothing at all; it is only included for backwards-compatibility with qore prior to version 0.8.0 for functions that would ignore type errors in arguments.

Restrictions:
Qore::PO_NO_THREAD_CONTROL, Qore::PO_NO_THREAD_INFO
Code Flags:
RUNTIME_NOOP
*TimeZone Qore::get_thread_tz ( )

Returns any TimeZone set for the current thread, NOTHING if none is set.

Returns
any TimeZone set for the current thread, NOTHING if none is set
Restrictions:
Qore::PO_NO_LOCALE_CONTROL
Example:
my *TimeZone $tz = get_thread_tz();
set_thread_tz(new TimeZone("Europe/Prague"));
on_exit set_thread_tz($tz);
See also
Since
Qore 0.8.5
hash Qore::getAllThreadCallStacks ( )

Returns a hash of call stacks keyed by each TID (thread ID)

Platform Availability:
Qore::Option::HAVE_RUNTIME_THREAD_STACK_TRACE
Restrictions:
Qore::PO_NO_THREAD_CONTROL, Qore::PO_NO_THREAD_INFO
Returns
a hash of call stacks keyed by each TID (thread ID); see call stacks for the format of the hash values
Example:
foreach my string $tid in ($cs.keyIterator()) {
printf("TID %d\n", $tid);
my int $i;
foreach my hash $l in ($cs.$tid)
if ($l.type != "new-thread")
printf(" %d: %s() called at %s:%d (%s function)\n", ++$i, $l.function, $l.file, $l.line, $l.type);
else
printf(" %d: *** thread started by background operator ***\n", ++$i);
}
int Qore::gettid ( )

Returns the Qore thread ID (TID) of the current thread.

Returns
the Qore thread ID (TID) of the current thread
Restrictions:
Qore::PO_NO_THREAD_INFO
Code Flags:
CONSTANT
Example:
my int $tid = gettid();
nothing Qore::mark_thread_resources ( )

Marks thread resources so that any thread resources left allocated after this call will be cleaned up when throw_thread_resource_exceptions_to_mark() is called.

When exceptions are thrown by this function, thread-local resources are also cleaned up at the same time.

Restrictions:
Qore::PO_NO_THREAD_CONTROL
Example:
try {
# calling the following function will ensure that any thread-resources
# allocated since the last call to mark_thread_resources() will be cleaned
# up and associated exceptions will be thrown
# ... some code or calls that may allocate thread resources
}
catch ($ex) {
# ... log or handle exceptions
}
See also
throwThreadResourceExceptions(), mark_thread_resources()
Since
Qore 0.8.4
int Qore::num_threads ( )

Returns the current number of threads in the process (not including the special signal handling thread)

Returns
the current number of threads in the process (not including the special signal handling thread)
Restrictions:
Qore::PO_NO_THREAD_INFO
Code Flags:
CONSTANT
Example:
my int $num = num_threads();
*hash Qore::remove_thread_data (   ...)

Removes the data associated to one or more keys in the thread-local data hash and returns the data removed.

Restrictions:
Qore::PO_NO_THREAD_CONTROL
Parameters
...the key names should be given directly in the argument list with this variant. If the given hash keys do not exist in the thread-local data hash, then the given key in the return value will have no value assigned
Returns
a hash of the data removed or NOTHING if no arguments were passed to the function
Example:
my hash $h = remove_thread_data("filename");
Since
Qore 0.8.4 this function returns the values it removes
hash Qore::remove_thread_data ( list  l)

Removes the data associated to one or more keys in the thread-local data hash from a literal list passed as the first argument and returns the data removed.

Restrictions:
Qore::PO_NO_THREAD_CONTROL
Parameters
la list of key names to remove from the thread-local data hash. If the given hash keys do not exist in the thread-local data hash, then the given key in the return value will have no value assigned
Returns
a hash of the data removed
Example:
my hash $h = remove_thread_data($key_list);
Since
Qore 0.8.4 this function returns the values it removes
nothing Qore::save_thread_data ( hash  h)

Saves the data passed in the thread-local hash; all keys are merged into the thread-local hash, overwriting any information that may have been there before.

Restrictions:
Qore::PO_NO_THREAD_CONTROL
Parameters
ha hash of data to save in the thread-local data hash
Example:
Note
This function does not throw any exceptions, however if a value is removed from the thread-local data hash by being overwritten with a new value, and the value is an object that goes out of scope, then such an object could throw an exception in its destructor
nothing Qore::save_thread_data ( string  key,
any  value 
)

Saves the data passed against the key passed as an argument in thread-local storage.

Restrictions:
Qore::PO_NO_THREAD_CONTROL
Parameters
keythe name of the key in the thread-local hash to save the data against
valuethe value to save in the thread-local hash against the key
Example:
save_thread_data("key1", $value);
Note
This function does not throw any exceptions, however if a value is removed from the thread-local data hash by being overwritten with a new value, and the value is an object that goes out of scope, then such an object could throw an exception in its destructor
nothing Qore::save_thread_data ( )

This function variant does nothing at all; it is only included for backwards-compatibility with qore prior to version 0.8.0 for functions that would ignore type errors in arguments.

Restrictions:
Qore::PO_NO_THREAD_CONTROL
Code Flags:
RUNTIME_NOOP
bool Qore::set_thread_init ( code  init)

Sets a call reference or closure to run every time a new thread is started.

This code can be used to initialize global thread-local variables, for example.

Returns
True if there was already user initialization code set, False if not
Restrictions:
Qore::PO_NO_THREAD_CONTROL
Example:
set_thread_init(sub () { $var = 123; });
Note
the code will be run for all new threads, but is not run by this function for the current thread
nothing Qore::set_thread_tz ( TimeZone  zone)

Sets the default time zone for the current thread.

Restrictions:
Qore::PO_NO_LOCALE_CONTROL
Parameters
zonethe TimeZone object for the current thread

This will cause the TimeZone::get() method to return the given TimeZone when called from the same thread

Example:
my *TimeZone $tz = get_thread_tz();
set_thread_tz(new TimeZone("Europe/Prague"));
on_exit set_thread_tz($tz);
Note
The TimeZone will only be set for the current thread in the current Program
See also
Since
Qore 0.8.5
nothing Qore::set_thread_tz ( )

Clears the thread-local time zone for the current thread; after this call TimeZone::get() will return the value set for the current Program.

Restrictions:
Qore::PO_NO_LOCALE_CONTROL
Example:
my *TimeZone $tz = get_thread_tz();
set_thread_tz(new TimeZone("Europe/Prague"));
on_exit set_thread_tz($tz);
Note
The TimeZone will only be cleared in the current thread in the current Program
See also
Since
Qore 0.8.5
list Qore::thread_list ( )

Returns a list of all current thread IDs.

Note that the special signal handling thread with TID 0 is never included in the list returned by this function

Returns
a list of all current thread IDs
Restrictions:
Qore::PO_NO_THREAD_INFO
Code Flags:
CONSTANT
Example:
my list $l = thread_list();
bool Qore::throw_thread_resource_exceptions_to_mark ( )

Immediately runs all thread resource cleanup routines for the current thread for thread resources created since the last call to mark_thread_resources() and throws all associated exceptions.

When exceptions are thrown by this function, thread-local resources are also cleaned up at the same time.

Returns
True if there are additional thread resource marks to process, False if there are no more
Restrictions:
Qore::PO_NO_THREAD_CONTROL
Example:
try {
# ... some code or calls that may allocate thread resources
# calling the following will ensure that any thread-resources allocated since
# the last call to mark_thread_resources() will be cleaned up and associated
# exceptions will be thrown
}
catch ($ex) {
# ... log or handle exceptions
}
See also
throwThreadResourceExceptions(), mark_thread_resources()
Since
Qore 0.8.4
nothing Qore::throwThreadResourceExceptions ( )

Immediately runs all thread resource cleanup routines for the current thread and throws all associated exceptions.

This function is particularly useful when used in combination with embedded code in order to catch (and log, for example) thread resource errors (ex: uncommitted transactions, unlocked locks, etc) - this can be used when control returns to the "master" program to ensure that no thread-local resources have been left active.

This function will run all thread resource cleanup routines even if mark_thread_resources() has been called (i.e. it clears all marks as well).

When exceptions are thrown by this function, thread-local resources are also cleaned up at the same time.

Restrictions:
Qore::PO_NO_THREAD_CONTROL
Example:
try {
}
catch ($ex) {
# ... log or handle exceptions
}
See also
mark_thread_resources(), throw_thread_resource_exceptions_to_mark()