LuciadCPillar 2024.0.08
luciad::CancellationToken Class Reference

This class contains information on whether an operation has been canceled. More...

#include <luciad/concurrent/CancellationToken.h>

Public Member Functions

bool isCanceled () const
 Returns whether the cancellation token has the status canceled. More...
 
void runTask (ICancellableTask &task) const
 Runs a cancellable task that will be notified when the CancellationToken is canceled. More...
 
template<typename RunFunction , typename CancelFunction >
void runTask (RunFunction &&run, CancelFunction &&cancel) const
 Runs a task in the form of a callable and notifies the caller if the CancellationToken is canceled. More...
 

Detailed Description

This class contains information on whether an operation has been canceled.

It is typically passed as a parameter to methods that perform an operation that could potentially take a long time. The method can either manually check whether the ongoing operation is canceled and react accordingly, or it can run a ICancellableTask on that token by calling CancellationToken::runTask, in which case the task will be notified as soon as the CancellationToken gets canceled.

For example: when raster data is added to the Map, the map's rendering backend will request raster tile data. When at the same time, this raster data is removed from the map, the rendering backend will want to stop requesting data as quickly as possible. For that purpose:

  • it sends out CancellationToken instances with each raster data request
  • it cancels the token at some point (for example when the raster layer is removed from the map)
  • code that performs the actual raster data request (for example using a http request) then has the opportunity to check if the request has been canceled or not by either: a. calling the CancellationToken::isCanceled method periodically and take appropriate action to stop executing once it noticed the token is canceled, or b. wrapping the Http request in an ICancellableTask and calling CancellationToken::runTask, so that the request can be aborted (by calling the appropriate method on the request class) as soon as the token gets canceled.

It is not mandatory to use the CancellationToken, but it is recommended. Not doing so may block operations for a longer time, such a removing a layer or closing the map.

Since
2022.1

Member Function Documentation

◆ isCanceled()

bool luciad::CancellationToken::isCanceled ( ) const

Returns whether the cancellation token has the status canceled.

This method is typically used by time-consuming operations. These operations are supposed to regularly check the status of this method. When it returns 'true', the operation can decide to stop executing.

Returns
the status of the cancellation token.

◆ runTask() [1/2]

void luciad::CancellationToken::runTask ( ICancellableTask task) const

Runs a cancellable task that will be notified when the CancellationToken is canceled.

This method synchronously calls ICancellableTask::run() on the provided task. If the token is canceled by another thread while runTask is executing, ICancellableTask::cancel() will be called on that other thread.

Parameters
taskthe cancellable task to be executed

◆ runTask() [2/2]

template<typename RunFunction , typename CancelFunction >
void luciad::CancellationToken::runTask ( RunFunction &&  run,
CancelFunction &&  cancel 
) const

Runs a task in the form of a callable and notifies the caller if the CancellationToken is canceled.

This method synchronously calls the provided run callable. If the token is canceled by another thread while runTask is executing, the cancel callable will be called on that other thread.

Parameters
runthe callable to be executed
cancelthe callable to execute when the token gets canceled