Qore Programming Language Reference Manual  0.9.3.2
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< softstring > 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< string, list< hash< CallStackInfo > > > Qore::getAllThreadCallStacks ()
 Returns a hash of lists of CallStackInfo hashes keyed by TID (thread ID) More...
 
hash< string, list< hash< CallStackInfo > > > Qore::get_all_thread_call_stacks ()
 Returns a hash of lists of CallStackInfo hashes keyed by TID (thread ID) More...
 
hash< auto > Qore::get_all_thread_data ()
 Returns the entire thread-local data hash. More...
 
int Qore::get_default_thread_stack_size ()
 returns the default thread stack size More...
 
int Qore::get_stack_size ()
 returns the current thread's stack size More...
 
list< hash< CallStackInfo > > Qore::get_thread_call_stack ()
 Returns a list of CallStackInfo hashes for the current TID (thread ID); because it is always from the same thread being read, no locking is applied as in get_all_thread_call_stacks() More...
 
auto 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...
 
*string Qore::get_thread_name ()
 returns the current thread's descriptive name if set or NOTHING if not 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< auto > 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< auto > Qore::remove_thread_data (list< softstring > 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...
 
bool Qore::remove_thread_resource (AbstractThreadResource resource)
 removes a thread resource from the current thread More...
 
bool Qore::remove_thread_resource (code resource)
 removes a callable thread resource from the current thread More...
 
nothing Qore::save_thread_data (hash< auto > 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, auto 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...
 
int Qore::set_default_thread_stack_size (int size)
 sets the default thread stack size for new threads created after this call and returns the stack size actually set 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_name (string name)
 sets the current thread's descriptive name More...
 
nothing Qore::set_thread_resource (AbstractThreadResource resource)
 sets a thread resource for the current thread More...
 
nothing Qore::set_thread_resource (code resource, auto arg)
 sets a callable thread resource for the current thread with an optional argument 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< intQore::thread_list ()
 Returns a list of all current thread IDs. More...
 
nothing Qore::thread_yield ()
 yields the current thread so other threads can be scheduled and run More...
 
nothing Qore::throwThreadResourceExceptions ()
 Immediately runs all thread resource cleanup routines for the current thread and throws all associated exceptions. More...
 
nothing Qore::throw_thread_resource_exceptions ()
 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

◆ delete_all_thread_data()

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

◆ delete_thread_data() [1/2]

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

◆ delete_thread_data() [2/2]

nothing Qore::delete_thread_data ( list< softstring >  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

◆ get_all_thread_call_stacks()

hash<string,list<hash<CallStackInfo> > > Qore::get_all_thread_call_stacks ( )

Returns a hash of lists of CallStackInfo hashes keyed by TID (thread ID)

Returns
a hash of lists of CallStackInfo hashes keyed by TID (thread ID)
Restrictions:
Qore::PO_NO_THREAD_CONTROL, Qore::PO_NO_THREAD_INFO
Example:
hash<string, list<hash<CallStackInfo>>> cs = get_all_thread_call_stacks();
foreach string tid in (cs.keyIterator()) {
printf("TID %d\n", tid);
int i;
foreach hash<CallStackInfo> 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);
}
}
}
Since
Qore 0.8.12 as a replacement for deprecated camel-case getAllThreadCallStacks()

◆ get_all_thread_data()

hash<auto> 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:
RET_VALUE_ONLY
Example:
hash<auto> th = get_all_thread_data();
Note
this function is not flagged with CONSTANT since its value could change at runtime

◆ get_default_thread_stack_size()

int Qore::get_default_thread_stack_size ( )

returns the default thread stack size

Platform Availability:
Qore::Option::HAVE_MANAGE_STACK
Restrictions:
Qore::PO_NO_THREAD_INFO
Code Flags:
CONSTANT
Example:
Note
if the thread stack size has been changed since this thread was started, then the value returned by this function will not match the current thread's stack size
Since
Qore 0.9.0

◆ get_stack_size()

int Qore::get_stack_size ( )

returns the current thread's stack size

Platform Availability:
Qore::Option::HAVE_GET_STACK_SIZE
Restrictions:
Qore::PO_NO_THREAD_INFO
Code Flags:
CONSTANT
Example:
int size = get_stack_size();
Returns
the current thread's stack size in bytes
Since
Qore 0.9.0

◆ get_thread_call_stack()

list<hash<CallStackInfo> > Qore::get_thread_call_stack ( )

Returns a list of CallStackInfo hashes for the current TID (thread ID); because it is always from the same thread being read, no locking is applied as in get_all_thread_call_stacks()

Returns
a list of CallStackInfo hashes for the current TID (thread ID)
Restrictions:
Qore::PO_NO_THREAD_CONTROL, Qore::PO_NO_THREAD_INFO
Example:
list<hash<CallStackInfo>> cs = get_thread_call_stack();
foreach hash<CallStackInfo> l in (cs) {
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);
}
}
Since
Qore 0.8.13

◆ get_thread_data() [1/2]

auto 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:
RET_VALUE_ONLY
Example:
auto data = get_thread_data("key1");

◆ get_thread_data() [2/2]

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

◆ get_thread_name()

*string Qore::get_thread_name ( )

returns the current thread's descriptive name if set or NOTHING if not

Platform Availability:
Qore::Option::HAVE_THREAD_NAME
Restrictions:
Qore::PO_NO_THREAD_CONTROL
Code Flags:
CONSTANT
Example:
*string name = get_stack_name();
Returns
the current thread's descriptive name if set or NOTHING if not
Note
  • if supported, the default thread name will be qore/tid where tid is the current thread ID
  • in case the current platform does not support thread names, NOTHING is always returned
Since
Qore 0.9.0

◆ get_thread_tz()

*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:
*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

◆ getAllThreadCallStacks()

hash<string,list<hash<CallStackInfo> > > Qore::getAllThreadCallStacks ( )

Returns a hash of lists of CallStackInfo hashes keyed by TID (thread ID)

Restrictions:
Qore::PO_NO_THREAD_CONTROL, Qore::PO_NO_THREAD_INFO
Code Flags:
DEPRECATED
Returns
a hash of lists of CallStackInfo hashes keyed by TID (thread ID)
Deprecated:
use get_all_thread_call_stacks(); camel-case function names were deprecated in Qore 0.8.12

◆ gettid()

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:
RET_VALUE_ONLY
Example:
int tid = gettid();
Note
this function is not flagged with CONSTANT since its value could change at runtime

◆ mark_thread_resources()

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 (hash<ExceptionInfo> ex) {
# ... log or handle exceptions
}
See also
Since
Qore 0.8.4

◆ num_threads()

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:
RET_VALUE_ONLY
Example:
int num = num_threads();
Note
this function is not flagged with CONSTANT since its value could change at runtime

◆ remove_thread_data() [1/2]

*hash<auto> 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:
hash<auto> h = remove_thread_data("filename");
Since
Qore 0.8.4 this function returns the values it removes

◆ remove_thread_data() [2/2]

hash<auto> Qore::remove_thread_data ( list< softstring >  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:
hash<auto> h = remove_thread_data(key_list);
Since
Qore 0.8.4 this function returns the values it removes

◆ remove_thread_resource() [1/2]

bool Qore::remove_thread_resource ( AbstractThreadResource  resource)

removes a thread resource from the current thread

Restrictions:
Qore::PO_NO_THREAD_CONTROL
Example:
Parameters
resourcethe thread resource to remove
Returns
True if the resource was removed, False if not
See also
Since
Qore 0.8.12

◆ remove_thread_resource() [2/2]

bool Qore::remove_thread_resource ( code  resource)

removes a callable thread resource from the current thread

Restrictions:
Qore::PO_NO_THREAD_CONTROL
Example:
Parameters
resourcethe callable thread resource to remove
Returns
True if the resource was removed, False if not
See also
Since
Qore 0.8.12

◆ save_thread_data() [1/3]

nothing Qore::save_thread_data ( hash< auto >  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

◆ save_thread_data() [2/3]

nothing Qore::save_thread_data ( string  key,
auto  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

◆ save_thread_data() [3/3]

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

◆ set_default_thread_stack_size()

int Qore::set_default_thread_stack_size ( int  size)

sets the default thread stack size for new threads created after this call and returns the stack size actually set

Platform Availability:
Qore::Option::HAVE_MANAGE_STACK
Restrictions:
Qore::PO_NO_THREAD_CONTROL
Example:
Parameters
sizethe size in bytes for new threads
Returns
the value actually set
Exceptions
SET-DEFAULT-THREAD-STACK-SIZE-ERRORan error occurred setting the default thread stack size
Since
Qore 0.9.0

◆ set_thread_init()

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.

Restrictions:
Qore::PO_NO_THREAD_CONTROL
Parameters
inita call reference or closure to run every time a new thread is started or NOTHING to clear any thread initialization code
Returns
True if there was already user initialization code set, False if not
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
See also
Qore::Program::setThreadInit()
Since
Qore 0.8.13 added the ability to remove the thread initialization code by passing NOTHING to this function

◆ set_thread_name()

nothing Qore::set_thread_name ( string  name)

sets the current thread's descriptive name

Platform Availability:
Qore::Option::HAVE_THREAD_NAME
Restrictions:
Qore::PO_NO_THREAD_CONTROL
Example:
Parameters
namethe new descriptive name for the thread
Since
Qore 0.9.0

◆ set_thread_resource() [1/2]

nothing Qore::set_thread_resource ( AbstractThreadResource  resource)

sets a thread resource for the current thread

Restrictions:
Qore::PO_NO_THREAD_CONTROL
Example:
Parameters
resourcethe thread resource to set
See also
Since
Qore 0.8.12

◆ set_thread_resource() [2/2]

nothing Qore::set_thread_resource ( code  resource,
auto  arg 
)

sets a callable thread resource for the current thread with an optional argument

Restrictions:
Qore::PO_NO_THREAD_CONTROL
Example:
set_thread_resource(\func(), code);
Parameters
resourcethe thread resource handler to set
argan argument to use when calling the thread resource handler
See also
Since
Qore 0.8.12

◆ set_thread_tz() [1/2]

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:
*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

◆ set_thread_tz() [2/2]

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:
*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

◆ thread_list()

list<int> 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:
RET_VALUE_ONLY
Example:
list<int> l = thread_list();
Note
this function is not flagged with CONSTANT since its value could change at runtime

◆ thread_yield()

nothing Qore::thread_yield ( )

yields the current thread so other threads can be scheduled and run

Restrictions:
Qore::PO_NO_PROCESS_CONTROL
Example:
Since
Qore 0.8.13

◆ throw_thread_resource_exceptions()

nothing Qore::throw_thread_resource_exceptions ( )

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 (hash<ExceptionInfo> ex) {
# ... log or handle exceptions
}
Note
may not throw an exception even if there are thread resources in place that get cleaned up in case the Qore::Thread::AbstractThreadResource::cleanup() method performs the cleanup but does not throw an exception
See also
Since
Qore 0.8.12 as a replacement for deprecated camel-case throwThreadResourceExceptions()

◆ throw_thread_resource_exceptions_to_mark()

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 (hash<<ExceptionInfo> ex) {
# ... log or handle exceptions
}
Note
may not throw an exception even if there are thread resources in place that get cleaned up in case the Qore::Thread::AbstractThreadResource::cleanup() method performs the cleanup but does not throw an exception
See also
Since
Qore 0.8.4

◆ throwThreadResourceExceptions()

nothing Qore::throwThreadResourceExceptions ( )

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

Restrictions:
Qore::PO_NO_THREAD_CONTROL
Code Flags:
DEPRECATED

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.

See also
Deprecated:
use throw_thread_resource_exceptions(); camel-case function names were deprecated in Qore 0.8.12