Cgu::Thread Namespace Reference

## Namespaces

| FutureHelper
| TaskManagerHelper

## Classes

struct | FutureThreadError
struct | FutureWhenError
class | Future
| A class representing a pthread thread which will provide a value. More...
struct | CondError
struct | MutexError
struct | RecMutexError
class | Mutex
| A wrapper class for pthread mutexes. More...
class | Cond
| A wrapper class for pthread condition variables. More...
class | RecMutex
| A wrapper class for pthread mutexes which provides a recursive mutex. More...
class | GrecmutexLock
| A scoped locking class for exception safe locking of GStaticRecMutex objects. More...
struct | ParallelError
struct | RWLockError
class | RWLock
| A wrapper class for pthread read-write locks. More...
struct | TaskError
class | TaskManager
| A thread-pool class for managing tasks in multi-threaded programs. More...
class | Thread
| A class representing a pthread thread. More...
class | JoinableHandle
| A class wrapping a Thread::Thread object representing a joinable thread. More...
class | CancelBlock
| A class enabling the cancellation state of a thread to be controlled. More...
class | Exit
| A class which can be thrown to terminate the throwing thread. More...

## Enumerations

enum | Locked { locked }
enum | DeferLock { defer }

## Functions

template<class Obj , class Ret , class... Params, class... Args>
Cgu::IntrusivePtr < Cgu::Thread::Future< Ret > > | make_future (Obj &obj, Ret(Obj::*func)(Params...), Args &&...args)
template<class Obj , class Ret , class... Params, class... Args>
Cgu::IntrusivePtr < Cgu::Thread::Future< Ret > > | make_future (const Obj &obj, Ret(Obj::*func)(Params...) const, Args &&...args)
template<class Ret , class... Params, class... Args>
Cgu::IntrusivePtr < Cgu::Thread::Future< Ret > > | make_future (Ret(*func)(Params...), Args &&...args)
template<class Ret , class Func >
Cgu::IntrusivePtr < Cgu::Thread::Future< Ret > > | make_future (Func &&func)
template<class Iterator , class Func >
void | parallel_for_each (TaskManager &tm, Iterator first, Iterator last, Func &&func)
template<class SourceIterator , class DestIterator , class Func >
void | parallel_transform (TaskManager &tm, SourceIterator first, SourceIterator last, DestIterator dest, Func &&func)
template<class SourceIterator1 , class SourceIterator2 , class DestIterator , class Func >
void | parallel_transform (TaskManager &tm, SourceIterator1 first1, SourceIterator1 last1, SourceIterator2 first2, DestIterator dest, Func &&func)
template<class Iterator , class Func >
Iterator | parallel_for_each_partial (TaskManager &tm, Iterator first, Iterator last, int max, Func &&func)
template<class SourceIterator , class DestIterator , class Func >
std::pair< SourceIterator, DestIterator > | parallel_transform_partial (TaskManager &tm, SourceIterator first, SourceIterator last, DestIterator dest, int max, Func &&func)
template<class SourceIterator1 , class SourceIterator2 , class DestIterator , class Func >
std::tuple< SourceIterator1, SourceIterator2, DestIterator > | parallel_transform_partial (TaskManager &tm, SourceIterator1 first1, SourceIterator1 last1, SourceIterator2 first2, DestIterator dest, int max, Func &&func)

## Enumeration Type Documentation

enum Cgu::Thread::DeferLock

Enumerator
defer |

enum Cgu::Thread::Locked

Enumerator
locked |

## Function Documentation

template<class Obj , class Ret , class... Params, class... Args>

Cgu::IntrusivePtr<Cgu::Thread::Future<Ret> > Cgu::Thread::make_future | ( | Obj & | obj,
|  | Ret(Obj::*)(Params...) | func,
|  | Args &&... | args
| ) |  |

Deprecated:

DEPRECATED. Use the version of make_future() which takes a callable object.

A convenience helper function which calls Cgu::Thread::Future::make() to obtain a Future object without the need to specify the return value of the function represented by the new object: that is deduced from the signature of that function. This is useful shorthand when also employed with the C++11 'auto' keyword.

Exceptions

  std::bad_alloc | It might throw std::bad_alloc if memory is exhausted and the system throws in that case. (This exception will not be thrown if the library has been installed using the --with-glib-memory-slices-no-compat configuration option: instead glib will terminate the program if it is unable to obtain memory from the operating system.)
  Cgu::Thread::MutexError | It might throw Cgu::Thread::MutexError if initialisation of the contained mutex fails. (It is often not worth checking for this, as it means either memory is exhausted or pthread has run out of other resources to create new mutexes.)
  Cgu::Thread::CondError | It might throw Cgu::Thread::CondError if initialisation of the contained condition variable fails. (It is often not worth checking for this, as it means either memory is exhausted or pthread has run out of other resources to create new condition variables.)

Note
  This method will also throw if the copy or move constructor of a bound argument throws, or the default constructor of the return value type of the function represented by the new object throws.

Since 2.0.4

template<class Obj , class Ret , class... Params, class... Args>

Cgu::IntrusivePtr<Cgu::Thread::Future<Ret> > Cgu::Thread::make_future | ( | const Obj & | obj,
|  | Ret(Obj::*)(Params...) const | func,
|  | Args &&... | args
| ) |  |

Deprecated:

DEPRECATED. Use the version of make_future() which takes a callable object.

A convenience helper function which calls Cgu::Thread::Future::make() to obtain a Future object without the need to specify the return value of the function represented by the new object: that is deduced from the signature of that function. This is useful shorthand when also employed with the C++11 'auto' keyword.

Exceptions

  std::bad_alloc | It might throw std::bad_alloc if memory is exhausted and the system throws in that case. (This exception will not be thrown if the library has been installed using the --with-glib-memory-slices-no-compat configuration option: instead glib will terminate the program if it is unable to obtain memory from the operating system.)
  Cgu::Thread::MutexError | It might throw Cgu::Thread::MutexError if initialisation of the contained mutex fails. (It is often not worth checking for this, as it means either memory is exhausted or pthread has run out of other resources to create new mutexes.)
  Cgu::Thread::CondError | It might throw Cgu::Thread::CondError if initialisation of the contained condition variable fails. (It is often not worth checking for this, as it means either memory is exhausted or pthread has run out of other resources to create new condition variables.)

Note
  This method will also throw if the copy or move constructor of a bound argument throws, or the default constructor of the return value type of the function represented by the new object throws.

Since 2.0.4

template<class Ret , class... Params, class... Args>

Cgu::IntrusivePtr<Cgu::Thread::Future<Ret> > Cgu::Thread::make_future | ( | Ret(*)(Params...) | func,
|  | Args &&... | args
| ) |  |

Deprecated:

DEPRECATED. Use the version of make_future() which takes a callable object.

A convenience helper function which calls Cgu::Thread::Future::make() to obtain a Future object without the need to specify the return value of the function represented by the new object: that is deduced from the signature of that function. This is useful shorthand when also employed with the C++11 'auto' keyword.

Exceptions

  std::bad_alloc | It might throw std::bad_alloc if memory is exhausted and the system throws in that case. (This exception will not be thrown if the library has been installed using the --with-glib-memory-slices-no-compat configuration option: instead glib will terminate the program if it is unable to obtain memory from the operating system.)
  Cgu::Thread::MutexError | It might throw Cgu::Thread::MutexError if initialisation of the contained mutex fails. (It is often not worth checking for this, as it means either memory is exhausted or pthread has run out of other resources to create new mutexes.)
  Cgu::Thread::CondError | It might throw Cgu::Thread::CondError if initialisation of the contained condition variable fails. (It is often not worth checking for this, as it means either memory is exhausted or pthread has run out of other resources to create new condition variables.)

Note
  This method will also throw if the copy or move constructor of a bound argument throws, or the default constructor of the return value type of the function represented by the new object throws.

Since 2.0.4

template<class Ret , class Func >

Cgu::IntrusivePtr<Cgu::Thread::Future<Ret> > Cgu::Thread::make_future | ( | Func && | func) |  |

A convenience helper function which calls Cgu::Thread::Future::make() to obtain a Future without the need to specify the return value of the callable object to be represented by it: that is deduced. This is useful shorthand when also employed with the C++11 'auto' keyword.

Parameters

  func | A callable object, such as formed by a lambda expression or the result of std::bind. It must be fully bound (that is, its must take no arguments when called). It should return a value (it cannot return void).

Exceptions

  std::bad_alloc | It might throw std::bad_alloc if memory is exhausted and the system throws in that case. (This exception will not be thrown if the library has been installed using the --with-glib-memory-slices-no-compat configuration option: instead glib will terminate the program if it is unable to obtain memory from the operating system.)
  Cgu::Thread::MutexError | It might throw Cgu::Thread::MutexError if initialisation of the contained mutex fails. (It is often not worth checking for this, as it means either memory is exhausted or pthread has run out of other resources to create new mutexes.)
  Cgu::Thread::CondError | It might throw Cgu::Thread::CondError if initialisation of the contained condition variable fails. (It is often not worth checking for this, as it means either memory is exhausted or pthread has run out of other resources to create new condition variables.)

Note
  1. This method will also throw if the copy or move constructor of the callable object passed as an argument throws, or the default constructor of the return value type of the function represented by the new object throws.
  2. If the callable object passed as an argument has both const and non-const operator()() methods, the non-const version will be called even if the callable object passed is a const object.

Since 2.0.14

template<class Iterator , class Func >

void Cgu::Thread::parallel_for_each | ( | TaskManager & | tm,
|  | Iterator | first,
|  | Iterator | last,
|  | Func && | func
| ) |  |

#include <c++-gtk-utils/parallel.h>

This function applies a callable object to each element of a container in the range ['first', 'last'), by executing each such application as a task of a Thread::TaskManager object. Tasks are added to the Thread::TaskManager object in the order in which the respective elements appear in the container (and if a task mutates its argument, it will do so in respect of the correct element of the container), but no other ordering arises, and the tasks will execute in parallel to the extent that the Thread::TaskManager object has sufficient threads available to do so.

Apart from that, and that this function returns void, it does the same as std::for_each(). It can mutate container elements if the callable object takes its argument by non-const reference. It will not return until the callable object has been applied to all of the elements in the range ['first', 'last').

This function can be called by a task running on the same TaskManager object.

Parameters

  tm | The Thread::TaskManager object on which the tasks will run.
  first | The beginning of the range to which 'func' is to be applied.
  last | One past the last element to which 'func' is to be applied.

  func

  A callable object to be applied to each element in the range ['first', 'last'), such as formed by a lambda expression or the result of std::bind. It should take a single unbound argument of the value type of the container to which 'first' and 'last' relate or a const or non-const reference to that type. Any return value is discarded. If an exception propagates from 'func', the exception will be consumed while the for each loop is running, and an attempt will still be made to apply 'func' to all remaining elements in the range ['first', 'last'), and only after that attempt has completed will the exception Cgu::Thread::ParallelError be thrown.

Exceptions

  std::bad_alloc | This exception will be thrown if memory is exhausted and the system throws in that case. (On systems with over-commit/lazy-commit combined with virtual memory (swap), it is rarely useful to check for memory exhaustion). If this exception is thrown, some tasks may nonetheless have already started by virtue of the call to this function, but subsequent ones will not.

  Cgu::Thread::TaskError

  This exception will be thrown if stop_all() has previously been called on the Thread::TaskManager object, or if another thread calls stop_all() after this method is called but before it has returned. It will also be thrown if the Thread::TaskManager object's is_error() method would return true because its internal thread pool loop implementation has thrown std::bad_alloc, or a thread has failed to start correctly because pthread has run out of resources. (On systems with over-commit/lazy-commit combined with virtual memory (swap), it is rarely useful to check for memory exhaustion, and if a reasonable maximum thread count has been chosen for the Thread::TaskManager object pthread should not run out of other resources, but there may be some specialized cases where the return value of is_error() is useful.) If this exception is thrown, some tasks may nonetheless have already started by virtue of the call to this function.

  Cgu::Thread::ParallelError

  This exception will be thrown if an exception propagates from the 'func' callable object when it executes on being applied to one or more elements of the container. Such an exception will not stop an attempt being made to apply 'func' (successfully or unsuccessfully) to all elements in the range ['first', 'last'). Cgu::Thread::ParallelError will be thrown after such attempted application has finished.

  Cgu::Thread::MutexError | This exception will be thrown if initialization of a mutex used by this function fails. (It is often not worth checking for this, as it means either memory is exhausted or pthread has run out of other resources to create new mutexes.) If this exception is thrown, no tasks will start.
  Cgu::Thread::CondError | This exception will be thrown if initialization of a condition variable used by this function fails. (It is often not worth checking for this, as it means either memory is exhausted or pthread has run out of other resources to create new condition variables.) If this exception is thrown, no tasks will start.

Note
  An exception might also be thrown if the copy or move constructor of the 'func' callable objects throws. If such an exception is thrown, no tasks will start.

Since 2.0.19/2.2.2

template<class Iterator , class Func >

Iterator Cgu::Thread::parallel_for_each_partial | ( | TaskManager & | tm,
|  | Iterator | first,
|  | Iterator | last,
|  | int | max,
|  | Func && | func
| ) |  |

#include <c++-gtk-utils/parallel.h>

This function applies a callable object to each element of a container in the range ['first', 'last') subject to a maximum, by executing each such application as a task of a Thread::TaskManager object. Tasks are added to the Thread::TaskManager object in the order in which the respective elements appear in the container (and if a task mutates its argument, it will do so in respect of the correct element of the container), but no other ordering arises, and the tasks will execute in parallel to the extent that the Thread::TaskManager object has sufficient threads available to do so.

This function does the same as Thread::parallel_for_each(), except that it returns an iterator to the element past the last element to which the callable object has been applied, and it has a 'max' parameter to limit the maximum number of elements to which the callable object will be applied on any one call to this function even if the range ['first', 'last') is greater than this maximum. Whether this limitation has had effect on any one call can be tested by checking whether the return value is equal to the 'last' parameter. If it is not, a further call to this function can be made.

The main purpose of this additional function is to enable the application of the callable object to the elements of a container to be dealt with in chunks, possibly to enable other tasks to be interleaved at reasonable intervals. For a container which does not support random access iterators, the 'last' parameter can be set to, say, the end of the container and the chunk size set with the 'max' paramater, with the return value being used as the 'first' parameter for subsequent calls to this function. This avoids having to increment the iterator for each "chunk" by stepping through the container by hand. In this usage, it therefore represents a minor efficiency improvement.

Parameters

  tm | The Thread::TaskManager object on which the tasks will run.
  first | The beginning of the range to which 'func' is to be applied.
  last | One past the last element to which 'func' is to be applied, subject to any maximum specified as the 'max' parameter.

  max

  The maximum number of elements of the source container to which 'func' will be applied on this call. It is not an error if it is greater than the distance between 'first' and 'last'. If it is equal to or greater than that distance, it follows that the iterator returned will be equal to 'last'. The same results if 'max' is a negative number (in that case no maximum will take effect and each element in the range ['first', 'last') will have 'func' applied to it).

  func

  A callable object to be applied (subject to the maximum) to each element in the range ['first', 'last'), such as formed by a lambda expression or the result of std::bind. It should take a single unbound argument of the value type of the container to which 'first' and 'last' relate or a const or non-const reference to that type. Any return value is discarded. If an exception propagates from 'func', the exception will be consumed while the for each loop is running, and an attempt will still be made to apply 'func' to all remaining elements in the range ['first', 'last') subject to the maximum, and only after that attempt has completed will the exception Cgu::Thread::ParallelError be thrown.

Returns
  An iterator representing the element past the last element of the range to which 'func' was applied, which may be passed as the 'first' argument of a subsequent call to this function.

Exceptions

  std::bad_alloc | This exception will be thrown if memory is exhausted and the system throws in that case. (On systems with over-commit/lazy-commit combined with virtual memory (swap), it is rarely useful to check for memory exhaustion). If this exception is thrown, some tasks may nonetheless have already started by virtue of the call to this function, but subsequent ones will not.

  Cgu::Thread::TaskError

  This exception will be thrown if stop_all() has previously been called on the Thread::TaskManager object, or if another thread calls stop_all() after this method is called but before it has returned. It will also be thrown if the Thread::TaskManager object's is_error() method would return true because its internal thread pool loop implementation has thrown std::bad_alloc, or a thread has failed to start correctly because pthread has run out of resources. (On systems with over-commit/lazy-commit combined with virtual memory (swap), it is rarely useful to check for memory exhaustion, and if a reasonable maximum thread count has been chosen for the Thread::TaskManager object pthread should not run out of other resources, but there may be some specialized cases where the return value of is_error() is useful.) If this exception is thrown, some tasks may nonetheless have already started by virtue of the call to this function.

  Cgu::Thread::ParallelError

  This exception will be thrown if an exception propagates from the 'func' callable object when it executes on being applied to one or more elements of the container. Such an exception will not stop an attempt being made to apply 'func' (successfully or unsuccessfully) to all elements in the range ['first', 'last') subject to the maximum. Cgu::Thread::ParallelError will be thrown after such attempted application has finished.

  Cgu::Thread::MutexError | This exception will be thrown if initialization of a mutex used by this function fails. (It is often not worth checking for this, as it means either memory is exhausted or pthread has run out of other resources to create new mutexes.) If this exception is thrown, no tasks will start.
  Cgu::Thread::CondError | This exception will be thrown if initialization of a condition variable used by this function fails. (It is often not worth checking for this, as it means either memory is exhausted or pthread has run out of other resources to create new condition variables.) If this exception is thrown, no tasks will start.

Note
  An exception might also be thrown if the copy or move constructor of the 'func' callable objects throws. If such an exception is thrown, no tasks will start.

Since 2.0.20/2.2.3

template<class SourceIterator , class DestIterator , class Func >

void Cgu::Thread::parallel_transform | ( | TaskManager & | tm,
|  | SourceIterator | first,
|  | SourceIterator | last,
|  | DestIterator | dest,
|  | Func && | func
| ) |  |

#include <c++-gtk-utils/parallel.h>

This function maps over a container in the range ['first', 'last'), applying a unary callable object to each element of the container in that range and storing the result in the destination range, by executing each such application as a task of a Thread::TaskManager object. Tasks are added to the Thread::TaskManager object in the order in which the respective elements appear in the source container, and the final result appears in the destination container in the same order as the source range from which it is generated (including if a back_inserter iterator is used), but no other ordering arises, and the tasks will execute in parallel to the extent that the Thread::TaskManager object has sufficient threads available to do so.

Apart from that, this function does the same as the version of std::transform() taking a unary function, except that it returns void (see Thread::parallel_transform_partial() for a function which returns a destination iterator and an iterator to the source range). It will not return until the callable object has been applied to all of the elements in the range ['first', 'last').

This function can be called by a task running on the same TaskManager object (a result can therefore be delivered asynchronously by calling this function in a task created by the make_task_when(), make_task_compose() or make_task_when_full() Thread::TaskManager methods). A task can carry out a map-reduce operation by passing the result of calling this function to std::accumulate() to perform a fold-left or fold-right on that result.

A separate overload of this function takes a binary callable object.

Here is a trivial example of a map-reduce operation which maps over a vector by multiplying each element by 2 in separate tasks, and then folds-left using std::accumulate() (std::accumulate() can fold using any callable object, but in this example the default of addition is used):

using namespace Cgu;
std::vector<int> v{1, 2, 3, 4, 5};
Thread::TaskManager tm;
Thread::parallel_transform(tm,
v.begin(),
v.end(),
v.begin(),
[] (int elt) {return elt * 2;});
// res will be equal to 30
int res = std::accumulate(v.begin(), v.end(), 0);

Parameters

  tm | The Thread::TaskManager object on which the tasks will run.
  first | The beginning of the range to which 'func' is to be applied.
  last | One past the last element to which 'func' is to be applied.
  dest | The beginning of the range to which the result of applying 'func' to the elements in the range ['first', 'last') is to be stored. As in the case of std::transform, this can overlap with or be the same as the source range. It may also be an insert iterator.

  func

  A unary callable object to be applied to each element in the range ['first', 'last'), such as formed by a lambda expression or the result of std::bind. It should take a single unbound argument of the value type of the container to which 'first' and 'last' relate or a const or non-const reference to that type. If an exception propagates from 'func', the exception will be consumed while the transform loop is running, and an attempt will still be made to apply 'func' to all remaining elements in the range ['first', 'last'), and only after that attempt has completed will the exception Cgu::Thread::ParallelError be thrown.

Exceptions

  std::bad_alloc | This exception will be thrown if memory is exhausted and the system throws in that case. (On systems with over-commit/lazy-commit combined with virtual memory (swap), it is rarely useful to check for memory exhaustion). If this exception is thrown, some tasks may nonetheless have already started by virtue of the call to this function, but subsequent ones will not.

  Cgu::Thread::TaskError

  This exception will be thrown if stop_all() has previously been called on the Thread::TaskManager object, or if another thread calls stop_all() after this method is called but before it has returned. It will also be thrown if the Thread::TaskManager object's is_error() method would return true because its internal thread pool loop implementation has thrown std::bad_alloc, or a thread has failed to start correctly because pthread has run out of resources. (On systems with over-commit/lazy-commit combined with virtual memory (swap), it is rarely useful to check for memory exhaustion, and if a reasonable maximum thread count has been chosen for the Thread::TaskManager object pthread should not run out of other resources, but there may be some specialized cases where the return value of is_error() is useful.) If this exception is thrown, some tasks may nonetheless have already started by virtue of the call to this function.

  Cgu::Thread::ParallelError

  This exception will be thrown if an exception propagates from the 'func' callable object when it executes on being applied to one or more elements of the source container. Such an exception will not stop an attempt being made to apply 'func' (successfully or unsuccessfully) to all elements in the range ['first', 'last'). Cgu::Thread::ParallelError will be thrown after such attempted application has finished.

  Cgu::Thread::MutexError | This exception will be thrown if initialization of a mutex used by this function fails. (It is often not worth checking for this, as it means either memory is exhausted or pthread has run out of other resources to create new mutexes.) If this exception is thrown, no tasks will start.
  Cgu::Thread::CondError | This exception will be thrown if initialization of a condition variable used by this function fails. (It is often not worth checking for this, as it means either memory is exhausted or pthread has run out of other resources to create new condition variables.) If this exception is thrown, no tasks will start.

Note
  An exception might also be thrown if the copy or move constructor of the 'func' callable objects throws. If such an exception is thrown, no tasks will start.

Since 2.0.19/2.2.2

template<class SourceIterator1 , class SourceIterator2 , class DestIterator , class Func >

void Cgu::Thread::parallel_transform | ( | TaskManager & | tm,
|  | SourceIterator1 | first1,
|  | SourceIterator1 | last1,
|  | SourceIterator2 | first2,
|  | DestIterator | dest,
|  | Func && | func
| ) |  |

#include <c++-gtk-utils/parallel.h>

This function maps over two containers, one in the range ['first1', 'last1') and the other beginning at 'first2', applying a binary callable object to each element of the containers in those ranges and storing the result in the destination range, by executing each such application as a task of a Thread::TaskManager object. Tasks are added to the Thread::TaskManager object in the order in which the respective elements appear in the source containers, and the final result appears in the destination container in the same order as the source ranges from which it is generated (including if a back_inserter iterator is used), but no other ordering arises, and the tasks will execute in parallel to the extent that the Thread::TaskManager object has sufficient threads available to do so.

Apart from that, this function does the same as the version of std::transform() taking a binary function, except that it returns void (see Thread::parallel_transform_partial() for a function which returns a destination iterator and iterators to the source ranges). It will not return until the callable object has been applied to all of the elements in the source ranges.

This function can be called by a task running on the same TaskManager object (a result can therefore be delivered asynchronously by calling this function in a task created by the make_task_when(), make_task_compose() or make_task_when_full() Thread::TaskManager methods). A task can carry out a map-reduce operation by passing the result of calling this function to std::accumulate() to perform a fold-left or fold-right on that result.

A separate overload of this function takes a unary callable object.

Here is a trivial example of a map-reduce operation which maps over two vectors by adding respective elements of the vectors in separate tasks, and then folds-left using std::accumulate() (std::accumulate() can fold using any callable object, but in this example the default of addition is used):

using namespace Cgu;
std::vector<int> v1{2, 4, 6, 8, 10};
std::vector<int> v2{10, 20, 30, 40, 50};
std::vector<int> v3;
Thread::TaskManager tm;
Thread::parallel_transform(tm,
v1.begin(),
v1.end(),
v2.begin(),
std::back_inserter(v3),
std::plus<int>());
// res will be equal to 180
int res = std::accumulate(v3.begin(), v3.end(), 0);

Parameters

  tm | The Thread::TaskManager object on which the tasks will run.
  first1 | The beginning of the range which is to be passed as the first argument of 'func'.
  last1 | One past the last element of the range which is to be passed as the first argument of 'func'.
  first2 | The beginning of the range which is to be passed as the second argument of 'func'.
  dest | The beginning of the range to which the result of applying 'func' to the elements in the source ranges is to be stored. As in the case of std::transform, this can overlap with or be the same as one of the source ranges. It may also be an insert iterator.

  func

  A binary callable object to be applied to each element in the source ranges, such as formed by a lambda expression or the result of std::bind. It should take two unbound arguments of the value types of the containers to which 'first1' and 'first2' relate or const or non-const references to those types. If an exception propagates from 'func', the exception will be consumed while the transform loop is running, and an attempt will still be made to apply 'func' to all remaining elements of the source ranges, and only after that attempt has completed will the exception Cgu::Thread::ParallelError be thrown.

Exceptions

  std::bad_alloc | This exception will be thrown if memory is exhausted and the system throws in that case. (On systems with over-commit/lazy-commit combined with virtual memory (swap), it is rarely useful to check for memory exhaustion). If this exception is thrown, some tasks may nonetheless have already started by virtue of the call to this function, but subsequent ones will not.

  Cgu::Thread::TaskError

  This exception will be thrown if stop_all() has previously been called on the Thread::TaskManager object, or if another thread calls stop_all() after this method is called but before it has returned. It will also be thrown if the Thread::TaskManager object's is_error() method would return true because its internal thread pool loop implementation has thrown std::bad_alloc, or a thread has failed to start correctly because pthread has run out of resources. (On systems with over-commit/lazy-commit combined with virtual memory (swap), it is rarely useful to check for memory exhaustion, and if a reasonable maximum thread count has been chosen for the Thread::TaskManager object pthread should not run out of other resources, but there may be some specialized cases where the return value of is_error() is useful.) If this exception is thrown, some tasks may nonetheless have already started by virtue of the call to this function.

  Cgu::Thread::ParallelError

  This exception will be thrown if an exception propagates from the 'func' callable object when it executes on being applied to one or more elements of the source ranges. Such an exception will not stop an attempt being made to apply 'func' (successfully or unsuccessfully) to all elements in the source ranges. Cgu::Thread::ParallelError will be thrown after such attempted application has finished.

  Cgu::Thread::MutexError | This exception will be thrown if initialization of a mutex used by this function fails. (It is often not worth checking for this, as it means either memory is exhausted or pthread has run out of other resources to create new mutexes.) If this exception is thrown, no tasks will start.
  Cgu::Thread::CondError | This exception will be thrown if initialization of a condition variable used by this function fails. (It is often not worth checking for this, as it means either memory is exhausted or pthread has run out of other resources to create new condition variables.) If this exception is thrown, no tasks will start.

Note
  An exception might also be thrown if the copy or move constructor of the 'func' callable objects throws. If such an exception is thrown, no tasks will start.

Since 2.0.19/2.2.2

template<class SourceIterator , class DestIterator , class Func >

std::pair<SourceIterator, DestIterator> Cgu::Thread::parallel_transform_partial | ( | TaskManager & | tm,
|  | SourceIterator | first,
|  | SourceIterator | last,
|  | DestIterator | dest,
|  | int | max,
|  | Func && | func
| ) |  |

#include <c++-gtk-utils/parallel.h>

This function maps over a container in the range ['first', 'last') subject to a maximum, applying a unary callable object to each element of the container in that range (subject to the maximum) and storing the result in the destination range, by executing each such application as a task of a Thread::TaskManager object. Tasks are added to the Thread::TaskManager object in the order in which the respective elements appear in the source container, and the final result appears in the destination container in the same order as the source range from which it is generated (including if a back_inserter iterator is used), but no other ordering arises, and the tasks will execute in parallel to the extent that the Thread::TaskManager object has sufficient threads available to do so.

A separate overload of this function takes a binary callable object.

This function does the same as the version of Thread::parallel_transform() taking a unary callable object, except that it returns a std::pair object containing an iterator to the element past the last element of the source range transformed, and a destination iterator to the element past the last element stored in the destination range, and it has a 'max' parameter to limit the maximum number of elements which will be so transformed on any one call to this function. Whether this limitation has had effect on any one call can be tested by checking whether the first value of the pair returned is equal to the 'last' parameter. If it is not, a further call to this function can be made.

The main purpose of this additional function is to enable the parallel transform of the elements of a container to be dealt with in chunks, possibly to enable other tasks to be interleaved at reasonable intervals. For source or destination containers which do not support random access iterators, the 'last' parameter can be set to, say, the end of the container and the chunk size set with the 'max' paramater, with the values of the returned pair being used as the 'first' and 'dest' parameters for subsequent calls to this function. This avoids having to increment the source and destination iterators for each "chunk" by stepping through the respective containers by hand. In this usage, it therefore represents a minor efficiency improvement.

Parameters

  tm | The Thread::TaskManager object on which the tasks will run.
  first | The beginning of the range to which 'func' is to be applied.
  last | One past the last element to which 'func' is to be applied, subject to any maximum specified as the 'max' parameter.
  dest | The beginning of the range to which the result of applying 'func' to the elements in the source range is to be stored. As in the case of std::transform, this can overlap with or be the same as the source range. It may also be an insert iterator.

  max

  The maximum number of elements of the source container which will be transformed on this call. It is not an error if it is greater than the distance between 'first' and 'last'. If it is equal to or greater than that distance, it follows that the first value of the pair returned by this function will be equal to 'last'. The same results if 'max' is a negative number (in that case no maximum will take effect and all elements in the range ['first', 'last') will be transformed), so passing -1 might be useful as a means of obtaining a destination iterator (the second value) for other purposes, particularly where it is not a random access iterator).

  func

  A unary callable object to be applied (subject to the maximum) to each element in the range ['first', 'last'), such as formed by a lambda expression or the result of std::bind. It should take a single unbound argument of the value type of the container to which 'first' and 'last' relate or a const or non-const reference to that type. If an exception propagates from 'func', the exception will be consumed while the transform loop is running, and an attempt will still be made to apply 'func' to all remaining elements in the range ['first', 'last') subject to the maximum, and only after that attempt has completed will the exception Cgu::Thread::ParallelError be thrown.

Returns
  A std::pair object. Its first member is an iterator representing the element past the last element of the source range transformed, which may be passed as the 'first' argument of a subsequent call to this function; and its second member is an iterator representing the element past the last element stored in the destination range, which may be passed as the 'dest' argument of the subsequent call.

Exceptions

  std::bad_alloc | This exception will be thrown if memory is exhausted and the system throws in that case. (On systems with over-commit/lazy-commit combined with virtual memory (swap), it is rarely useful to check for memory exhaustion). If this exception is thrown, some tasks may nonetheless have already started by virtue of the call to this function, but subsequent ones will not.

  Cgu::Thread::TaskError

  This exception will be thrown if stop_all() has previously been called on the Thread::TaskManager object, or if another thread calls stop_all() after this method is called but before it has returned. It will also be thrown if the Thread::TaskManager object's is_error() method would return true because its internal thread pool loop implementation has thrown std::bad_alloc, or a thread has failed to start correctly because pthread has run out of resources. (On systems with over-commit/lazy-commit combined with virtual memory (swap), it is rarely useful to check for memory exhaustion, and if a reasonable maximum thread count has been chosen for the Thread::TaskManager object pthread should not run out of other resources, but there may be some specialized cases where the return value of is_error() is useful.) If this exception is thrown, some tasks may nonetheless have already started by virtue of the call to this function.

  Cgu::Thread::ParallelError

  This exception will be thrown if an exception propagates from the 'func' callable object when it executes on being applied to one or more elements of the source container. Such an exception will not stop an attempt being made to apply 'func' (successfully or unsuccessfully) to all elements in the range ['first', 'last') subject to the maximum. Cgu::Thread::ParallelError will be thrown after such attempted application has finished.

  Cgu::Thread::MutexError | This exception will be thrown if initialization of a mutex used by this function fails. (It is often not worth checking for this, as it means either memory is exhausted or pthread has run out of other resources to create new mutexes.) If this exception is thrown, no tasks will start.
  Cgu::Thread::CondError | This exception will be thrown if initialization of a condition variable used by this function fails. (It is often not worth checking for this, as it means either memory is exhausted or pthread has run out of other resources to create new condition variables.) If this exception is thrown, no tasks will start.

Note
  An exception might also be thrown if the copy or move constructor of the 'func' callable objects throws. If such an exception is thrown, no tasks will start.

Since 2.0.20/2.2.3

template<class SourceIterator1 , class SourceIterator2 , class DestIterator , class Func >

std::tuple<SourceIterator1, SourceIterator2, DestIterator> Cgu::Thread::parallel_transform_partial | ( | TaskManager & | tm,
|  | SourceIterator1 | first1,
|  | SourceIterator1 | last1,
|  | SourceIterator2 | first2,
|  | DestIterator | dest,
|  | int | max,
|  | Func && | func
| ) |  |

#include <c++-gtk-utils/parallel.h>

This function maps over two containers, one in the range ['first1', 'last1') subject to a maximum, and the other beginning at 'first2', applying a binary callable object to each element of the containers in those ranges (subject to the maximum) and storing the result in the destination range, by executing each such application as a task of a Thread::TaskManager object. Tasks are added to the Thread::TaskManager object in the order in which the respective elements appear in the source containers, and the final result appears in the destination container in the same order as the source ranges from which it is generated (including if a back_inserter iterator is used), but no other ordering arises, and the tasks will execute in parallel to the extent that the Thread::TaskManager object has sufficient threads available to do so.

A separate overload of this function takes a unary callable object.

This function does the same as the version of Thread::parallel_transform() taking a binary callable object, except that it returns a std::tuple object containing iterators to the element past the last elements of the source ranges transformed, and a destination iterator to the element past the last element stored in the destination range, and it has a 'max' parameter to limit the maximum number of elements which will be so transformed on any one call to this function. Whether this limitation has had effect on any one call can be tested by checking whether the first value of the tuple returned is equal to the 'last' parameter. If it is not, a further call to this function can be made.

The main purpose of this additional function is to enable the parallel transform of the elements of a container to be dealt with in chunks, possibly to enable other tasks to be interleaved at reasonable intervals. For source or destination containers which do not support random access iterators, the 'last' parameter can be set to, say, the end of the container and the chunk size set with the 'max' paramater, with the values of the returned tuple being used as the 'first1', 'first2' and 'dest' parameters for subsequent calls to this function. This avoids having to increment the source and destination iterators for each "chunk" by stepping through the respective containers by hand. In this usage, it therefore represents a minor efficiency improvement.

Parameters

  tm | The Thread::TaskManager object on which the tasks will run.
  first1 | The beginning of the range which is to be passed as the first argument of 'func'.
  last1 | One past the last element of the range which is to be passed as the first argument of 'func', subject to any maximum specified as the 'max' parameter.
  first2 | The beginning of the range which is to be passed as the second argument of 'func'.
  dest | The beginning of the range to which the result of applying 'func' to the elements in the source ranges is to be stored. As in the case of std::transform, this can overlap with or be the same as one of the source ranges. It may also be an insert iterator.

  max

  The maximum number of elements of the source containers which will be transformed on this call. It is not an error if it is greater than the distance between 'first1' and 'last1'. If it is equal to or greater than that distance, it follows that the first value of the tuple returned by this function will be equal to 'last1'. The same results if 'max' is a negative number (in that case no maximum will take effect and all elements in the range ['first1', 'last1') will be transformed), so passing -1 might be useful as a means of obtaining a second source iterator (the second value of the tuple) or destination iterator (the third value) for other purposes, particularly where they are not random access iterators.

  func

  A binary callable object to be applied (subject to the maximum) to each element in the source ranges, such as formed by a lambda expression or the result of std::bind. It should take two unbound arguments of the value types of the containers to which 'first1' and 'first2' relate or const or non-const references to those types. If an exception propagates from 'func', the exception will be consumed while the transform loop is running, and an attempt will still be made to apply 'func' to all remaining elements of the source ranges subject to the maximum, and only after that attempt has completed will the exception Cgu::Thread::ParallelError be thrown.

Returns
  A std::tuple object. Its first value is an iterator representing the element past the last element of the first source range transformed, which may be passed as the 'first1' argument of a subsequent call to this function; its second value is an iterator representing the element past the last element of the second source range transformed, which may be passed as the 'first2' argument of the subsequent call; and its third value is an iterator representing the element past the last element stored in the destination range, which may be passed as the 'dest' argument of the subsequent call.

Exceptions

  std::bad_alloc | This exception will be thrown if memory is exhausted and the system throws in that case. (On systems with over-commit/lazy-commit combined with virtual memory (swap), it is rarely useful to check for memory exhaustion). If this exception is thrown, some tasks may nonetheless have already started by virtue of the call to this function, but subsequent ones will not.

  Cgu::Thread::TaskError

  This exception will be thrown if stop_all() has previously been called on the Thread::TaskManager object, or if another thread calls stop_all() after this method is called but before it has returned. It will also be thrown if the Thread::TaskManager object's is_error() method would return true because its internal thread pool loop implementation has thrown std::bad_alloc, or a thread has failed to start correctly because pthread has run out of resources. (On systems with over-commit/lazy-commit combined with virtual memory (swap), it is rarely useful to check for memory exhaustion, and if a reasonable maximum thread count has been chosen for the Thread::TaskManager object pthread should not run out of other resources, but there may be some specialized cases where the return value of is_error() is useful.) If this exception is thrown, some tasks may nonetheless have already started by virtue of the call to this function.

  Cgu::Thread::ParallelError

  This exception will be thrown if an exception propagates from the 'func' callable object when it executes on being applied to one or more elements of the source ranges. Such an exception will not stop an attempt being made to apply 'func' (successfully or unsuccessfully) to all elements in the source ranges subject to the maximum. Cgu::Thread::ParallelError will be thrown after such attempted application has finished.

  Cgu::Thread::MutexError | This exception will be thrown if initialization of a mutex used by this function fails. (It is often not worth checking for this, as it means either memory is exhausted or pthread has run out of other resources to create new mutexes.) If this exception is thrown, no tasks will start.
  Cgu::Thread::CondError | This exception will be thrown if initialization of a condition variable used by this function fails. (It is often not worth checking for this, as it means either memory is exhausted or pthread has run out of other resources to create new condition variables.) If this exception is thrown, no tasks will start.

Note
  An exception might also be thrown if the copy or move constructor of the 'func' callable objects throws. If such an exception is thrown, no tasks will start.

Since 2.0.20/2.2.3