c++-gtk-utils
future.h
Go to the documentation of this file.
1 /* Copyright (C) 2010 to 2012 Chris Vine
2 
3 The library comprised in this file or of which this file is part is
4 distributed by Chris Vine under the GNU Lesser General Public
5 License as follows:
6 
7  This library is free software; you can redistribute it and/or
8  modify it under the terms of the GNU Lesser General Public License
9  as published by the Free Software Foundation; either version 2.1 of
10  the License, or (at your option) any later version.
11 
12  This library is distributed in the hope that it will be useful, but
13  WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  Lesser General Public License, version 2.1, for more details.
16 
17  You should have received a copy of the GNU Lesser General Public
18  License, version 2.1, along with this library (see the file LGPL.TXT
19  which came with this source code package in the src/utils sub-directory);
20  if not, write to the Free Software Foundation, Inc.,
21  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 
23 However, it is not intended that the object code of a program whose
24 source code instantiates a template from this file or uses macros or
25 inline functions (of any length) should by reason only of that
26 instantiation or use be subject to the restrictions of use in the GNU
27 Lesser General Public License. With that in mind, the words "and
28 macros, inline functions and instantiations of templates (of any
29 length)" shall be treated as substituted for the words "and small
30 macros and small inline functions (ten lines or less in length)" in
31 the fourth paragraph of section 5 of that licence. This does not
32 affect any other reason why object code may be subject to the
33 restrictions in that licence (nor for the avoidance of doubt does it
34 affect the application of section 2 of that licence to modifications
35 of the source code in this file).
36 
37 */
38 
39 #ifndef CGU_FUTURE_H
40 #define CGU_FUTURE_H
41 
42 #include <memory>
43 #include <exception>
44 #include <functional> // for std::function
45 #include <utility> // for std::move and std::forward
46 
47 #include <pthread.h>
48 #include <glib.h>
49 
50 #include <c++-gtk-utils/thread.h>
51 #include <c++-gtk-utils/mutex.h>
52 #include <c++-gtk-utils/callback.h>
55 #include <c++-gtk-utils/emitter.h>
56 #include <c++-gtk-utils/timeout.h>
58 
59 namespace Cgu {
60 
61 namespace Thread {
62 
63 struct FutureThreadError: public std::exception {
64  virtual const char* what() const throw() {return "FutureThreadError\n";}
65 };
66 
67 struct FutureWhenError: public std::exception {
68  virtual const char* what() const throw() {return "FutureWhenError\n";}
69 };
70 
71 /**
72  * @class Cgu::Thread::Future future.h c++-gtk-utils/future.h
73  * @brief A class representing a pthread thread which will
74  * provide a value.
75  * @sa Cgu::Thread::Thread Cgu::Thread::JoinableHandle Cgu::AsyncResult Cgu::Thread::make_future() Cgu::Thread::TaskManager
76  *
77  * The Thread::Future class will launch a worker thread, run the
78  * function it represents in that thread until it returns, and store
79  * the return value so that it can be waited on and/or extracted by
80  * another thread. A new Thread::Future object representing the
81  * function to be called is created by calling
82  * Cgu::Thread::Future<>::make() (a static member function) or from
83  * version 2.0.4 the Cgu::Thread::make_future() helper function. The
84  * worker thread is then started by calling run(), and the value
85  * extracted or waited for by calling get(). The run() method can
86  * only be called once, but any number of threads can wait for and/or
87  * extract the return value by calling the get() method. The class
88  * also provides a SafeEmitter @ref DoneEmitterAnchor "done_emitter"
89  * public object which emits when the worker thread has finished, and
90  * an associated when() function. In addition, from version 2.0.11 a
91  * move_get() method is provided, which is discussed further below.
92  *
93  * The template parameter type of Thread::Future is the type of the
94  * return value of the function called by the Thread::Future object.
95  * The return value can be any type, including any arbitrarily large
96  * tuple or other struct or standard C++ container (but see below
97  * about copying).
98  *
99  * A Thread::Future object cannot represent a function with a void
100  * return type - a compilation error will result if that is attempted.
101  * If no return value is wanted, then the Thread::Thread class can be
102  * used directly. (However, if in a particular usage this class is
103  * thought to be more convenient, the function to be represented by it
104  * can be wrapped by another function which provides a dummy return
105  * value, such as a dummy int. One possible case for this is where
106  * more than one thread wants to wait for the worker thread to
107  * terminate, as pthread_join() and so Thread::Thread::join() only
108  * give defined behaviour when called by one thread.) In addition, a
109  * Thread::Future object cannot represent a function with a non-const
110  * reference argument - that would not normally be safe, and a compile
111  * error will be generated if that is attempted. However, from
112  * version 2.0.0-rc3, const reference arguments can be bound to
113  * Thread::Future objects (this is safe, as the Thread::Future object
114  * will keep its own copy of the argument passed to it).
115  *
116  * The make() method and make_future() functions take a plain
117  * function, static member function or non-static member function,
118  * which can take up to three arguments in the case of a non-static
119  * member function, and four arguments in the case of any other
120  * function. In the case of a non-static member function, the
121  * referenced object whose member function is to be called must remain
122  * in existence until the worker thread has completed. Alternatively,
123  * a std::function object can be passed, which can have any number of
124  * arguments using std::bind (and which can also bind the referenced
125  * object of a non-static member function by taking a copy of it where
126  * that is necessary).
127  *
128  * It is to be noted that the target function to be represented by a
129  * Thread::Future object must not allow any exception other than
130  * Thread::Exit, an exception deriving from std::exception or a
131  * cancellation pseudo-exception to escape from it when it is
132  * executed. This includes ensuring that, for any argument of that
133  * function which is of class type and not taken by reference to
134  * const, the argument type's copy constructor does not throw anything
135  * other than these. (If the target function or the copy constructor
136  * of a value argument throws Thread::Exit or an exception deriving
137  * from std::exception, the exception is safely consumed and the
138  * Thread::Future object's error flag is set.)
139  *
140  * Where a std::function object is not passed, internal moving/copying
141  * of arguments for the target function to be represented by the
142  * Thread::Future object takes place (once by invoking the rvalue move
143  * constructor or lvalue copy constructor, as appropriate, when make()
144  * or make_future() are called and, if the argument is not a const
145  * reference argument, once in the worker thread when run() is
146  * called). Therefore, if a non-trivial class object is to be
147  * received by the target function as an argument, it is best either
148  * (a) if it has a move constructor, to pass it to make() or
149  * make_future() as a temporary and have the target function take a
150  * const reference argument, or (b) for it to be constructed on free
151  * store and for the target function to receive it by pointer, by
152  * Cgu::SharedLockPtr, or by a std::shared_ptr implementation which
153  * has a thread-safe reference count. Note also that constructing
154  * std::function objects using std::bind can cause a number of copies
155  * of arguments to be made, so for ordinary usage it is better to pass
156  * a function pointer with arguments to make() or make_future() rather
157  * than a std::function object.
158  *
159  * Copying of the return value of the target function represented by
160  * the Thread::Future object also takes place. The run() method will
161  * store the return value of that function, so that it is available to
162  * the get() and move_get() methods and any 'when' callback, and
163  * therefore copy it once (unless, that is, the target function's
164  * return value type has a move assignment operator, in which case
165  * where possible that operator is called).
166  *
167  * For safety reasons, the get() method returns by value and so will
168  * cause the return value to be copied once more, so for return values
169  * comprising complex class objects which are to be abstracted using
170  * the get() method, it is often better if the function represented by
171  * the Thread::Future object allocates the return value on free store
172  * and returns it by pointer, by Cgu::SharedLockPtr, or by a
173  * std::shared_ptr implementation which has a thread-safe reference
174  * count. Alternatively, from version 2.0.11 a move_get() method is
175  * provided which will make a move operation instead of a copy if the
176  * return value's type implements a move constructor, but see the
177  * documentation on move_get() for the caveats with respect to its
178  * use: in particular, if move_get() is to be called by a thread, then
179  * get() may not normally be called by another thread, nor should the
180  * when() method be called.
181  *
182  * It should be noted that where the when() method is used, the return
183  * value is passed to the 'when' callback by reference to const and so
184  * without the copying carried out by the get() method: therefore, if
185  * the return value has a move assignment operator and the when()
186  * method is to be employed, and the 'when' callback only needs to
187  * call const methods of the return value, it may be more efficient
188  * not to allocate the return value on free store.
189  *
190  * This is a usage example:
191  *
192  * @code
193  * class Numbers {
194  * public:
195  * std::vector<long> get_primes(int n); // calculates the first n primes
196  * // and puts them in a vector
197  * ...
198  * };
199  *
200  * Numbers obj;
201  *
202  * // get the first 1,000 primes
203  * using namespace Cgu;
204  *
205  * auto future = Thread::make_future(obj, &Numbers::get_primes, 1000);
206  * // Thread::make_future() is a wrapper for the equivalent long-hand version required prior to version 2.0.4:
207  * // auto future = Thread::Future<std::vector<long>>::make(obj, &Numbers::get_primes, 1000);
208  *
209  * future->run();
210  * ... [ do something else ] ...
211  * std::vector<long> result(future->move_get());
212  * std::for_each(result.begin(), result.end(), [](long l) {std::cout << l << std::endl;});
213  * @endcode
214  *
215  * If get_primes() were a static member function or plain function,
216  * the syntax would be:
217  *
218  * @code
219  * auto future = Thread::make_future(&Numbers::get_primes, 1000);
220  * @endcode
221  *
222  * @b The @b Cgu::Thread::Future::when() @b functions
223  *
224  * From version 2.0.2, the return value of the thread function
225  * represented by Cgu::Thread::Future can be obtained asynchronously
226  * using Cgu::Thread::Future::when() to execute a function in a glib
227  * main loop when the thread function completes. The above example
228  * could be reimplemented as:
229  *
230  * @code
231  * class Numbers {
232  * public:
233  * std::vector<long> get_primes(int n); // calculates the first n primes
234  * // and puts them in a vector
235  * ...
236  * };
237  *
238  * void print_primes(const std::vector<long>& result) {
239  * std::for_each(result.begin(), result.end(), [](long l) {std::cout << l << std::endl;});
240  * }
241  *
242  * Numbers obj;
243  *
244  * using namespace Cgu;
245  *
246  * auto future = Thread::make_future(obj, &Numbers::get_primes, 1000);
247  * future->when(Callback::make(&print_primes));
248  * future->run();
249  * @endcode
250  *
251  * In this example, the callback which prints the primes to the
252  * console would execute in the default program main loop once the
253  * thread function providing those primes returns. As an alternative
254  * to a free standing print_primes() function, the callback object
255  * could be constructed from a C++11 lambda expression using
256  * Cgu::Callback::lambda() (available from version 2.0.9).
257  *
258  * @b Lambda @b functions
259  *
260  * As mentioned above, Cgu::Thread::Future objects can be constructed
261  * from std::function objects. Because C++11 lambda expressions are
262  * implicitly convertible to std::function objects and the only type
263  * to be resolved by the Cgu::Thread::Future::make() and
264  * Cgu::Thread::make_future() factory functions for such objects is
265  * the return type, this means that those functions can be passed a
266  * lambda expression, as for example:
267  *
268  * @code
269  * using namespace Cgu;
270  * int i = 1;
271  * auto f = Thread::Future<int>::make([=]() {return i + 10;});
272  * f->run();
273  * std::cout << f->get() << std::endl;
274  * @endcode
275  *
276  * However, if make_future() is used, the return type parameter must
277  * be explicitly stated:
278  *
279  * @code
280  * using namespace Cgu;
281  * int i = 1;
282  * auto f = Thread::make_future<int>([=]() {return i + 10;});
283  * f->run();
284  * std::cout << f->get() << std::endl;
285  * @endcode
286  *
287  * Lambda expressions which do not capture a variable from their
288  * environment are also convertible to a function pointer, but in the
289  * absence of an explicit cast the conversion to std::function object
290  * will be preferred.
291  *
292  * @b Overloaded @b functions
293  *
294  * Where a member function or ordinary function represented by a
295  * Thread::Future object is overloaded, this will cause difficulties
296  * in template type deduction when Thread::Future<>::make() or
297  * Thread::make_future() are called. With Thread::Future<>::make(),
298  * functions may be overloaded on numbers of argument without
299  * difficulty, but not with Thread::make_future(). For example:
300  * @code
301  * class Numbers {
302  * public:
303  * int calc(int i);
304  * int calc(int i, int j);
305  * ...
306  * };
307  *
308  * Numbers obj;
309  *
310  * using namespace Cgu;
311  *
312  * int i = 1, j = 2;
313  *
314  * auto f1 =
315  * Thread::Future<int>::make(obj, &Numbers::calc, i); // OK
316  * auto f2 =
317  * Thread::Future<int>::make(obj, &Numbers::calc, i, j); // OK
318  *
319  * // explicit is disambiguation required with Thread::make_future()
320  * auto f3 =
321  * Thread::make_future(obj, static_cast<int(Numbers::*)(int)>(&Numbers::calc), i);
322  * auto f4 =
323  * Thread::make_future(obj, static_cast<int(Numbers::*)(int, int)>(&Numbers::calc), i, j);
324  * @endcode
325  * Neither Thread::Future<>::make() nor Thread::make_future() can be
326  * overloaded on types of argument without explicit disambiguation.
327  * For example:
328  * @code
329  * class Numbers {
330  * public:
331  * int calc(int i);
332  * int calc(double d);
333  * ...
334  * };
335  *
336  * Numbers obj;
337  *
338  * using namespace Cgu;
339  *
340  * int i = 1;
341  * double d = 2.0;
342  *
343  * auto f1 =
344  * Thread::Future<int>::make(obj, static_cast<int (Numbers::*)(int)>(&Numbers::calc), i);
345  * auto f2 =
346  * Thread::Future<int>::make(obj, static_cast<int (Numbers::*)(double)>(&Numbers::calc), d);
347  * auto f3 =
348  * Thread::make_future(obj, static_cast<int (Numbers::*)(int)>(&Numbers::calc), i);
349  * auto f4 =
350  * Thread::make_future(obj, static_cast<int (Numbers::*)(double)>(&Numbers::calc), d);
351  * @endcode
352  */
353 
354 template <class Val>
356 
357  std::unique_ptr<Cgu::Thread::Thread> thread_u;
358  std::unique_ptr<Cgu::Callback::Callback> cb_u;
359 
360  mutable Mutex mutex;
361  Cond cond;
362  Val val;
363  bool done;
364  bool running;
365  bool error;
366  bool emitter_error;
367 
368  template <class T, class Ret, class... Args>
369  void run_wrapper(T*, Ret (T::*)(Args...), const Args&...);
370 
371  template <class T, class Ret, class... Args>
372  void run_wrapper_const(const T*, Ret (T::*)(Args...) const, const Args&...);
373 
374  template <class Ret, class... Args>
375  void run_wrapper_static(Ret (*)(Args...), const Args&...);
376 
377  void run_wrapper_func(const std::function<Val(void)>&);
378 
379  void cancel_cleanup();
380 
381  void execute_done(const Cgu::Callback::SafeFunctorArg<const Val&>&);
382  void post_done(const Cgu::Callback::SafeFunctorArg<const Val&>&,
383  gint, GMainContext*);
384  void execute_done_rel(const Cgu::SharedLockPtr<Cgu::SafeEmitterArg<const Val&>>&);
385  void post_done_rel(const Cgu::SharedLockPtr<Cgu::SafeEmitterArg<const Val&>>&,
386  gint, GMainContext*);
387 
388  // this is a static function taking the future object by IntrusivePtr to
389  // ensure that the future object remains in existence whilst this
390  // function might execute
391  static void fail_cb(const Cgu::IntrusivePtr<Future<Val>>& future,
392  const Cgu::Callback::SafeFunctor& func,
393  bool& ret);
394 
395  // private constructor - this class can only be created with Thread::Future::make()
396  Future(): val(), done(false), running(false), error(false), emitter_error(false) {}
397 
398 public:
399 
400  // this class cannot be copied except by smart pointer
401 /**
402  * This class cannot be copied (except by smart pointer). The copy
403  * constructor is deleted.
404  */
405  Future(const Future&) = delete;
406 
407 /**
408  * This class cannot be copied (except by smart pointer). The
409  * assignment operator is deleted.
410  */
411  Future& operator=(const Future&) = delete;
412 
413 /**
414  * Constructs a new Cgu::Thread::Future object (returned by
415  * Cgu::IntrusivePtr<Cgu::Thread::Future<Val>>). The type parameter
416  * Val represents the return value of the function to be represented
417  * by the new object. From version 2.0.4, it will usually be more
418  * convenient to call the Cgu::Thread::make_future() function, which
419  * is a convenience wrapper for this static method.
420  * @exception std::bad_alloc It might throw std::bad_alloc if memory
421  * is exhausted and the system throws in that case. (This exception
422  * will not be thrown if the library has been installed using the
423  * --with-glib-memory-slices-no-compat configuration option: instead
424  * glib will terminate the program if it is unable to obtain memory
425  * from the operating system.)
426  * @exception Cgu::Thread::MutexError It might throw
427  * Cgu::Thread::MutexError if initialisation of the contained mutex
428  * fails. (It is often not worth checking for this, as it means
429  * either memory is exhausted or pthread has run out of other
430  * resources to create new mutexes.)
431  * @exception Cgu::Thread::CondError It might throw
432  * Cgu::Thread::CondError if initialisation of the contained condition
433  * variable fails. (It is often not worth checking for this, as it
434  * means either memory is exhausted or pthread has run out of other
435  * resources to create new condition variables.)
436  * @note This method will also throw if the default constructor of the
437  * return value type throws.
438  */
439  template <class Ret, class T>
441  Ret (T::*func)());
442 
443 /**
444  * Constructs a new Cgu::Thread::Future object (returned by
445  * Cgu::IntrusivePtr<Cgu::Thread::Future<Val>>). The type parameter
446  * Val represents the return value of the function to be represented
447  * by the new object. From version 2.0.4, it will usually be more
448  * convenient to call the Cgu::Thread::make_future() function, which
449  * is a convenience wrapper for this static method.
450  * @exception std::bad_alloc It might throw std::bad_alloc if memory
451  * is exhausted and the system throws in that case. (This exception
452  * will not be thrown if the library has been installed using the
453  * --with-glib-memory-slices-no-compat configuration option: instead
454  * glib will terminate the program if it is unable to obtain memory
455  * from the operating system.)
456  * @exception Cgu::Thread::MutexError It might throw
457  * Cgu::Thread::MutexError if initialisation of the contained mutex
458  * fails. (It is often not worth checking for this, as it means
459  * either memory is exhausted or pthread has run out of other
460  * resources to create new mutexes.)
461  * @exception Cgu::Thread::CondError It might throw
462  * Cgu::Thread::CondError if initialisation of the contained condition
463  * variable fails. (It is often not worth checking for this, as it
464  * means either memory is exhausted or pthread has run out of other
465  * resources to create new condition variables.)
466  * @note This method will also throw if the copy or move constructor
467  * of the bound argument throws, or the default constructor of the
468  * return value type throws.
469  */
470  template <class Ret, class Param1, class Arg1, class T>
472  Ret (T::*func)(Param1),
473  Arg1&& arg1);
474 
475 /**
476  * Constructs a new Cgu::Thread::Future object (returned by
477  * Cgu::IntrusivePtr<Cgu::Thread::Future<Val>>). The type parameter
478  * Val represents the return value of the function to be represented
479  * by the new object. From version 2.0.4, it will usually be more
480  * convenient to call the Cgu::Thread::make_future() function, which
481  * is a convenience wrapper for this static method.
482  * @exception std::bad_alloc It might throw std::bad_alloc if memory
483  * is exhausted and the system throws in that case. (This exception
484  * will not be thrown if the library has been installed using the
485  * --with-glib-memory-slices-no-compat configuration option: instead
486  * glib will terminate the program if it is unable to obtain memory
487  * from the operating system.)
488  * @exception Cgu::Thread::MutexError It might throw
489  * Cgu::Thread::MutexError if initialisation of the contained mutex
490  * fails. (It is often not worth checking for this, as it means
491  * either memory is exhausted or pthread has run out of other
492  * resources to create new mutexes.)
493  * @exception Cgu::Thread::CondError It might throw
494  * Cgu::Thread::CondError if initialisation of the contained condition
495  * variable fails. (It is often not worth checking for this, as it
496  * means either memory is exhausted or pthread has run out of other
497  * resources to create new condition variables.)
498  * @note This method will also throw if the copy or move constructor
499  * of a bound argument throws, or the default constructor of the
500  * return value type throws.
501  */
502  template <class Ret, class Param1, class Param2, class Arg1, class Arg2, class T>
504  Ret (T::*func)(Param1, Param2),
505  Arg1&& arg1,
506  Arg2&& arg2);
507 
508 /**
509  * Constructs a new Cgu::Thread::Future object (returned by
510  * Cgu::IntrusivePtr<Cgu::Thread::Future<Val>>). The type parameter
511  * Val represents the return value of the function to be represented
512  * by the new object. From version 2.0.4, it will usually be more
513  * convenient to call the Cgu::Thread::make_future() function, which
514  * is a convenience wrapper for this static method.
515  * @exception std::bad_alloc It might throw std::bad_alloc if memory
516  * is exhausted and the system throws in that case. (This exception
517  * will not be thrown if the library has been installed using the
518  * --with-glib-memory-slices-no-compat configuration option: instead
519  * glib will terminate the program if it is unable to obtain memory
520  * from the operating system.)
521  * @exception Cgu::Thread::MutexError It might throw
522  * Cgu::Thread::MutexError if initialisation of the contained mutex
523  * fails. (It is often not worth checking for this, as it means
524  * either memory is exhausted or pthread has run out of other
525  * resources to create new mutexes.)
526  * @exception Cgu::Thread::CondError It might throw
527  * Cgu::Thread::CondError if initialisation of the contained condition
528  * variable fails. (It is often not worth checking for this, as it
529  * means either memory is exhausted or pthread has run out of other
530  * resources to create new condition variables.)
531  * @note This method will also throw if the copy or move constructor
532  * of a bound argument throws, or the default constructor of the
533  * return value type throws.
534  */
535  template <class Ret, class Param1, class Param2, class Param3,
536  class Arg1, class Arg2, class Arg3, class T>
538  Ret (T::*func)(Param1, Param2, Param3),
539  Arg1&& arg1,
540  Arg2&& arg2,
541  Arg3&& arg3);
542 
543 /**
544  * Constructs a new Cgu::Thread::Future object (returned by
545  * Cgu::IntrusivePtr<Cgu::Thread::Future<Val>>). The type parameter
546  * Val represents the return value of the function to be represented
547  * by the new object. From version 2.0.4, it will usually be more
548  * convenient to call the Cgu::Thread::make_future() function, which
549  * is a convenience wrapper for this static method.
550  * @exception std::bad_alloc It might throw std::bad_alloc if memory
551  * is exhausted and the system throws in that case. (This exception
552  * will not be thrown if the library has been installed using the
553  * --with-glib-memory-slices-no-compat configuration option: instead
554  * glib will terminate the program if it is unable to obtain memory
555  * from the operating system.)
556  * @exception Cgu::Thread::MutexError It might throw
557  * Cgu::Thread::MutexError if initialisation of the contained mutex
558  * fails. (It is often not worth checking for this, as it means
559  * either memory is exhausted or pthread has run out of other
560  * resources to create new mutexes.)
561  * @exception Cgu::Thread::CondError It might throw
562  * Cgu::Thread::CondError if initialisation of the contained condition
563  * variable fails. (It is often not worth checking for this, as it
564  * means either memory is exhausted or pthread has run out of other
565  * resources to create new condition variables.)
566  * @note This method will also throw if the default constructor of the
567  * return value type throws.
568  */
569  template <class Ret, class T>
571  Ret (T::*func)() const);
572 
573 /**
574  * Constructs a new Cgu::Thread::Future object (returned by
575  * Cgu::IntrusivePtr<Cgu::Thread::Future<Val>>). The type parameter
576  * Val represents the return value of the function to be represented
577  * by the new object. From version 2.0.4, it will usually be more
578  * convenient to call the Cgu::Thread::make_future() function, which
579  * is a convenience wrapper for this static method.
580  * @exception std::bad_alloc It might throw std::bad_alloc if memory
581  * is exhausted and the system throws in that case. (This exception
582  * will not be thrown if the library has been installed using the
583  * --with-glib-memory-slices-no-compat configuration option: instead
584  * glib will terminate the program if it is unable to obtain memory
585  * from the operating system.)
586  * @exception Cgu::Thread::MutexError It might throw
587  * Cgu::Thread::MutexError if initialisation of the contained mutex
588  * fails. (It is often not worth checking for this, as it means
589  * either memory is exhausted or pthread has run out of other
590  * resources to create new mutexes.)
591  * @exception Cgu::Thread::CondError It might throw
592  * Cgu::Thread::CondError if initialisation of the contained condition
593  * variable fails. (It is often not worth checking for this, as it
594  * means either memory is exhausted or pthread has run out of other
595  * resources to create new condition variables.)
596  * @note This method will also throw if the copy or move constructor
597  * of the bound argument throws, or the default constructor of the
598  * return value type throws.
599  */
600  template <class Ret, class Param1, class Arg1, class T>
602  Ret (T::*func)(Param1) const,
603  Arg1&& arg1);
604 
605 /**
606  * Constructs a new Cgu::Thread::Future object (returned by
607  * Cgu::IntrusivePtr<Cgu::Thread::Future<Val>>). The type parameter
608  * Val represents the return value of the function to be represented
609  * by the new object. From version 2.0.4, it will usually be more
610  * convenient to call the Cgu::Thread::make_future() function, which
611  * is a convenience wrapper for this static method.
612  * @exception std::bad_alloc It might throw std::bad_alloc if memory
613  * is exhausted and the system throws in that case. (This exception
614  * will not be thrown if the library has been installed using the
615  * --with-glib-memory-slices-no-compat configuration option: instead
616  * glib will terminate the program if it is unable to obtain memory
617  * from the operating system.)
618  * @exception Cgu::Thread::MutexError It might throw
619  * Cgu::Thread::MutexError if initialisation of the contained mutex
620  * fails. (It is often not worth checking for this, as it means
621  * either memory is exhausted or pthread has run out of other
622  * resources to create new mutexes.)
623  * @exception Cgu::Thread::CondError It might throw
624  * Cgu::Thread::CondError if initialisation of the contained condition
625  * variable fails. (It is often not worth checking for this, as it
626  * means either memory is exhausted or pthread has run out of other
627  * resources to create new condition variables.)
628  * @note This method will also throw if the copy or move constructor
629  * of a bound argument throws, or the default constructor of the
630  * return value type throws.
631  */
632  template <class Ret, class Param1, class Param2, class Arg1, class Arg2, class T>
634  Ret (T::*func)(Param1, Param2) const,
635  Arg1&& arg1,
636  Arg2&& arg2);
637 
638 /**
639  * Constructs a new Cgu::Thread::Future object (returned by
640  * Cgu::IntrusivePtr<Cgu::Thread::Future<Val>>). The type parameter
641  * Val represents the return value of the function to be represented
642  * by the new object. From version 2.0.4, it will usually be more
643  * convenient to call the Cgu::Thread::make_future() function, which
644  * is a convenience wrapper for this static method.
645  * @exception std::bad_alloc It might throw std::bad_alloc if memory
646  * is exhausted and the system throws in that case. (This exception
647  * will not be thrown if the library has been installed using the
648  * --with-glib-memory-slices-no-compat configuration option: instead
649  * glib will terminate the program if it is unable to obtain memory
650  * from the operating system.)
651  * @exception Cgu::Thread::MutexError It might throw
652  * Cgu::Thread::MutexError if initialisation of the contained mutex
653  * fails. (It is often not worth checking for this, as it means
654  * either memory is exhausted or pthread has run out of other
655  * resources to create new mutexes.)
656  * @exception Cgu::Thread::CondError It might throw
657  * Cgu::Thread::CondError if initialisation of the contained condition
658  * variable fails. (It is often not worth checking for this, as it
659  * means either memory is exhausted or pthread has run out of other
660  * resources to create new condition variables.)
661  * @note This method will also throw if the copy or move constructor
662  * of a bound argument throws, or the default constructor of the
663  * return value type throws.
664  */
665  template <class Ret, class Param1, class Param2, class Param3,
666  class Arg1, class Arg2, class Arg3, class T>
668  Ret (T::*func)(Param1, Param2, Param3) const,
669  Arg1&& arg1,
670  Arg2&& arg2,
671  Arg3&& arg3);
672 
673 /**
674  * Constructs a new Cgu::Thread::Future object (returned by
675  * Cgu::IntrusivePtr<Cgu::Thread::Future<Val>>). The type parameter
676  * Val represents the return value of the function to be represented
677  * by the new object. From version 2.0.4, it will usually be more
678  * convenient to call the Cgu::Thread::make_future() function, which
679  * is a convenience wrapper for this static method.
680  * @exception std::bad_alloc It might throw std::bad_alloc if memory
681  * is exhausted and the system throws in that case. (This exception
682  * will not be thrown if the library has been installed using the
683  * --with-glib-memory-slices-no-compat configuration option: instead
684  * glib will terminate the program if it is unable to obtain memory
685  * from the operating system.)
686  * @exception Cgu::Thread::MutexError It might throw
687  * Cgu::Thread::MutexError if initialisation of the contained mutex
688  * fails. (It is often not worth checking for this, as it means
689  * either memory is exhausted or pthread has run out of other
690  * resources to create new mutexes.)
691  * @exception Cgu::Thread::CondError It might throw
692  * Cgu::Thread::CondError if initialisation of the contained condition
693  * variable fails. (It is often not worth checking for this, as it
694  * means either memory is exhausted or pthread has run out of other
695  * resources to create new condition variables.)
696  * @note This method will also throw if the default constructor of the
697  * return value type throws.
698  */
699  template <class Ret>
700  static Cgu::IntrusivePtr<Cgu::Thread::Future<Val>> make(Ret (*func)());
701 
702 /**
703  * Constructs a new Cgu::Thread::Future object (returned by
704  * Cgu::IntrusivePtr<Cgu::Thread::Future<Val>>). The type parameter
705  * Val represents the return value of the function to be represented
706  * by the new object. From version 2.0.4, it will usually be more
707  * convenient to call the Cgu::Thread::make_future() function, which
708  * is a convenience wrapper for this static method.
709  * @exception std::bad_alloc It might throw std::bad_alloc if memory
710  * is exhausted and the system throws in that case. (This exception
711  * will not be thrown if the library has been installed using the
712  * --with-glib-memory-slices-no-compat configuration option: instead
713  * glib will terminate the program if it is unable to obtain memory
714  * from the operating system.)
715  * @exception Cgu::Thread::MutexError It might throw
716  * Cgu::Thread::MutexError if initialisation of the contained mutex
717  * fails. (It is often not worth checking for this, as it means
718  * either memory is exhausted or pthread has run out of other
719  * resources to create new mutexes.)
720  * @exception Cgu::Thread::CondError It might throw
721  * Cgu::Thread::CondError if initialisation of the contained condition
722  * variable fails. (It is often not worth checking for this, as it
723  * means either memory is exhausted or pthread has run out of other
724  * resources to create new condition variables.)
725  * @note This method will also throw if the copy or move constructor
726  * of the bound argument throws, or the default constructor of the
727  * return value type throws.
728  */
729  template <class Ret, class Param1, class Arg1>
730  static Cgu::IntrusivePtr<Cgu::Thread::Future<Val>> make(Ret (*func)(Param1),
731  Arg1&& arg1);
732 
733 /**
734  * Constructs a new Cgu::Thread::Future object (returned by
735  * Cgu::IntrusivePtr<Cgu::Thread::Future<Val>>). The type parameter
736  * Val represents the return value of the function to be represented
737  * by the new object. From version 2.0.4, it will usually be more
738  * convenient to call the Cgu::Thread::make_future() function, which
739  * is a convenience wrapper for this static method.
740  * @exception std::bad_alloc It might throw std::bad_alloc if memory
741  * is exhausted and the system throws in that case. (This exception
742  * will not be thrown if the library has been installed using the
743  * --with-glib-memory-slices-no-compat configuration option: instead
744  * glib will terminate the program if it is unable to obtain memory
745  * from the operating system.)
746  * @exception Cgu::Thread::MutexError It might throw
747  * Cgu::Thread::MutexError if initialisation of the contained mutex
748  * fails. (It is often not worth checking for this, as it means
749  * either memory is exhausted or pthread has run out of other
750  * resources to create new mutexes.)
751  * @exception Cgu::Thread::CondError It might throw
752  * Cgu::Thread::CondError if initialisation of the contained condition
753  * variable fails. (It is often not worth checking for this, as it
754  * means either memory is exhausted or pthread has run out of other
755  * resources to create new condition variables.)
756  * @note This method will also throw if the copy or move constructor
757  * of a bound argument throws, or the default constructor of the
758  * return value type throws.
759  */
760  template <class Ret, class Param1, class Param2, class Arg1, class Arg2>
761  static Cgu::IntrusivePtr<Cgu::Thread::Future<Val>> make(Ret (*func)(Param1, Param2),
762  Arg1&& arg1,
763  Arg2&& arg2);
764 
765 /**
766  * Constructs a new Cgu::Thread::Future object (returned by
767  * Cgu::IntrusivePtr<Cgu::Thread::Future<Val>>). The type parameter
768  * Val represents the return value of the function to be represented
769  * by the new object. From version 2.0.4, it will usually be more
770  * convenient to call the Cgu::Thread::make_future() function, which
771  * is a convenience wrapper for this static method.
772  * @exception std::bad_alloc It might throw std::bad_alloc if memory
773  * is exhausted and the system throws in that case. (This exception
774  * will not be thrown if the library has been installed using the
775  * --with-glib-memory-slices-no-compat configuration option: instead
776  * glib will terminate the program if it is unable to obtain memory
777  * from the operating system.)
778  * @exception Cgu::Thread::MutexError It might throw
779  * Cgu::Thread::MutexError if initialisation of the contained mutex
780  * fails. (It is often not worth checking for this, as it means
781  * either memory is exhausted or pthread has run out of other
782  * resources to create new mutexes.)
783  * @exception Cgu::Thread::CondError It might throw
784  * Cgu::Thread::CondError if initialisation of the contained condition
785  * variable fails. (It is often not worth checking for this, as it
786  * means either memory is exhausted or pthread has run out of other
787  * resources to create new condition variables.)
788  * @note This method will also throw if the copy or move constructor
789  * of a bound argument throws, or the default constructor of the
790  * return value type throws.
791  */
792  template <class Ret, class Param1, class Param2, class Param3,
793  class Arg1, class Arg2, class Arg3>
794  static Cgu::IntrusivePtr<Cgu::Thread::Future<Val>> make(Ret (*func)(Param1, Param2, Param3),
795  Arg1&& arg1,
796  Arg2&& arg2,
797  Arg3&& arg3);
798 
799 /**
800  * Constructs a new Cgu::Thread::Future object (returned by
801  * Cgu::IntrusivePtr<Cgu::Thread::Future<Val>>). The type parameter
802  * Val represents the return value of the function to be represented
803  * by the new object. From version 2.0.4, it will usually be more
804  * convenient to call the Cgu::Thread::make_future() function, which
805  * is a convenience wrapper for this static method.
806  * @exception std::bad_alloc It might throw std::bad_alloc if memory
807  * is exhausted and the system throws in that case. (This exception
808  * will not be thrown if the library has been installed using the
809  * --with-glib-memory-slices-no-compat configuration option: instead
810  * glib will terminate the program if it is unable to obtain memory
811  * from the operating system.)
812  * @exception Cgu::Thread::MutexError It might throw
813  * Cgu::Thread::MutexError if initialisation of the contained mutex
814  * fails. (It is often not worth checking for this, as it means
815  * either memory is exhausted or pthread has run out of other
816  * resources to create new mutexes.)
817  * @exception Cgu::Thread::CondError It might throw
818  * Cgu::Thread::CondError if initialisation of the contained condition
819  * variable fails. (It is often not worth checking for this, as it
820  * means either memory is exhausted or pthread has run out of other
821  * resources to create new condition variables.)
822  * @note This method will also throw if the copy or move constructor
823  * of a bound argument throws, or the default constructor of the
824  * return value type throws.
825  */
826  template <class Ret, class Param1, class Param2, class Param3, class Param4,
827  class Arg1, class Arg2, class Arg3, class Arg4>
828  static Cgu::IntrusivePtr<Cgu::Thread::Future<Val>> make(Ret (*func)(Param1, Param2, Param3, Param4),
829  Arg1&& arg1,
830  Arg2&& arg2,
831  Arg3&& arg3,
832  Arg4&& arg4);
833 
834 /**
835  * Constructs a new Cgu::Thread::Future object (returned by
836  * Cgu::IntrusivePtr<Cgu::Thread::Future<Val>>). The type parameter
837  * Val represents the return value of the function to be represented
838  * by the new object. From version 2.0.4, it will usually be more
839  * convenient to call the Cgu::Thread::make_future() function, which
840  * is a convenience wrapper for this static method.
841  * @exception std::bad_alloc It might throw std::bad_alloc if memory
842  * is exhausted and the system throws in that case. (This exception
843  * will not be thrown if the library has been installed using the
844  * --with-glib-memory-slices-no-compat configuration option: instead
845  * glib will terminate the program if it is unable to obtain memory
846  * from the operating system.)
847  * @exception Cgu::Thread::MutexError It might throw
848  * Cgu::Thread::MutexError if initialisation of the contained mutex
849  * fails. (It is often not worth checking for this, as it means
850  * either memory is exhausted or pthread has run out of other
851  * resources to create new mutexes.)
852  * @exception Cgu::Thread::CondError It might throw
853  * Cgu::Thread::CondError if initialisation of the contained condition
854  * variable fails. (It is often not worth checking for this, as it
855  * means either memory is exhausted or pthread has run out of other
856  * resources to create new condition variables.)
857  * @note This method will also throw if the copy or move constructor
858  * of a bound argument throws, or the default constructor of the
859  * return value type throws.
860  */
861  static Cgu::IntrusivePtr<Cgu::Thread::Future<Val>> make(const std::function<Val(void)>& func);
862 
863 /**
864  * Constructs a new Cgu::Thread::Future object (returned by
865  * Cgu::IntrusivePtr<Cgu::Thread::Future<Val>>). The type parameter
866  * Val represents the return value of the function to be represented
867  * by the new object. From version 2.0.4, it will usually be more
868  * convenient to call the Cgu::Thread::make_future() function, which
869  * is a convenience wrapper for this static method.
870  * @exception std::bad_alloc It might throw std::bad_alloc if memory
871  * is exhausted and the system throws in that case. (This exception
872  * will not be thrown if the library has been installed using the
873  * --with-glib-memory-slices-no-compat configuration option: instead
874  * glib will terminate the program if it is unable to obtain memory
875  * from the operating system.)
876  * @exception Cgu::Thread::MutexError It might throw
877  * Cgu::Thread::MutexError if initialisation of the contained mutex
878  * fails. (It is often not worth checking for this, as it means
879  * either memory is exhausted or pthread has run out of other
880  * resources to create new mutexes.)
881  * @exception Cgu::Thread::CondError It might throw
882  * Cgu::Thread::CondError if initialisation of the contained condition
883  * variable fails. (It is often not worth checking for this, as it
884  * means either memory is exhausted or pthread has run out of other
885  * resources to create new condition variables.)
886  * @note This method will also throw if the copy or move constructor
887  * of a bound argument throws, or the default constructor of the
888  * return value type throws.
889  */
890  static Cgu::IntrusivePtr<Cgu::Thread::Future<Val>> make(std::function<Val(void)>&& func);
891 
892 /**
893  * Runs the function represented by this Cgu::Thread::Future object in
894  * a new worker thread. That function will only be run once. If this
895  * is the first time this method has been called, it will start the
896  * worker thread and return true, and if it has previously been
897  * called, this method will do nothing and return false. This method
898  * is thread safe and may be called by a different thread from the one
899  * which called make().
900  * @return true if this is the first time this method has been called,
901  * or false if this method has previously been called.
902  * @exception Cgu::Thread::FutureThreadError This method might throw
903  * Cgu::Thread::FutureThreadError if it has not previously been called
904  * and the thread did not start properly. If it does throw, this
905  * Cgu::Thread::Future object is defunct and further attempts to call
906  * this method will return immediately with a false value. (It is
907  * often not worth checking for this exception, as it means either
908  * memory is exhausted, the pthread thread limit has been reached or
909  * pthread has run out of other resources to start new threads.)
910  * @exception std::bad_alloc This method might throw std::bad_alloc if
911  * it has not previously been called, and memory is exhausted and the
912  * system throws in that case. If it does throw, this
913  * Cgu::Thread::Future object is defunct and further attempts to call
914  * this method will return immediately with a false value. (This
915  * exception will not be thrown if the library has been installed
916  * using the --with-glib-memory-slices-no-compat configuration option:
917  * instead glib will terminate the program if it is unable to obtain
918  * memory from the operating system.)
919  * @note 1. Any Cgu::Thread::Exit exception, or any uncaught exception
920  * derived from std::exception, which is thrown from the worker thread
921  * will be caught and consumed and the error flag will be set. The
922  * worker thread will safely terminate and unwind the stack in so
923  * doing.
924  * @note 2. As this wrapper class can provide error reporting in a way
925  * that Cgu::Thread::Thread of itself cannot, it would be desirable to
926  * consume any other uncaught exceptions. However, this cannot be
927  * done: annoyingly, NPTL's forced stack unwinding does not allow this
928  * if thread cancellation is to be made available. If an uncaught
929  * exception propagates out of a thread when the thread exits, the
930  * C++11 standard will cause std::terminate() to be called and so
931  * terminate the entire program. Accordingly, a user must make sure
932  * that no exceptions, other than Cgu::Thread::Exit or those derived
933  * from std::exception or any cancellation pseudo-exception, can
934  * propagate from the function which this Cgu::Thread::Future object
935  * represents.
936  * @note 3. If the worker thread is cancelled by a call to cancel()
937  * while in the middle of executing the function which this
938  * Cgu::Thread::Future object represents, the error flag will be set.
939  */
940  bool run();
941 
942 /**
943  * Gets the stored value obtained from the function which is
944  * represented by this object. If the worker thread launched by the
945  * call to run() has not completed, then this method will block until
946  * it has completed. If run() has not been called, then run() will be
947  * called (and this method will block until the launched worker thread
948  * completes). If the function which is represented by this object
949  * throws Cgu::Thread::Exit or an uncaught exception derived from
950  * std::exception, then the exception will have been consumed by this
951  * Cgu::Thread::Future object and the error flag will have been set.
952  * The error flag will also have been set if the worker thread is
953  * cancelled or the thread wrapper in this Cgu::Thread::Future object
954  * threw std::bad_alloc. This method is thread safe and may be called
955  * by any thread (and by more than one thread). It is a cancellation
956  * point if it blocks, and from version 2.0.11 is cancellation safe if
957  * the stack unwinds on cancellation. It is also strongly exception
958  * safe: no data will be lost if extracting the value fails.
959  * @return The value obtained from the function which is represented
960  * by this object
961  * @exception Cgu::Thread::FutureThreadError This method might throw
962  * Cgu::Thread::FutureThreadError if run() has not previously been
963  * called and the thread did not start properly when this function
964  * called run().
965  * @exception std::bad_alloc This method might throw std::bad_alloc if
966  * run() has not previously been called, memory is exhausted and the
967  * system throws in that case. (This exception will not be thrown if
968  * the library has been installed using the
969  * --with-glib-memory-slices-no-compat configuration option: instead
970  * glib will terminate the program if it is unable to obtain memory
971  * from the operating system.)
972  * @note 1. This method might also throw if the copy constructor of
973  * the returned value type throws.
974  * @note 2. Question: Couldn't this method return the stored value by
975  * lvalue reference to const? Answer: It could. However, because of
976  * return value optimization, which will be implemented by any
977  * compiler capable of compiling this library, no advantage would be
978  * gained by doing so when initializing a local variable with the
979  * return value of this method (the copy constructor will only be
980  * called once whether returning by value or const reference). The
981  * advantage of returning by value is that the call to the copy
982  * constructor is forced to be within this Thread::Future object's
983  * mutex, so different threads' calls to the copy constructor are
984  * serialized, and also with blocked cancellation, so this method is
985  * cancellation safe. All calls to this method by different threads
986  * are therefore isolated and we do not have to worry about the thread
987  * safety of direct access to the stored value via its const methods
988  * outside the mutex (which would not be thread safe if the stored
989  * value has data members declared mutable) nor about the cancellation
990  * safety of the copy constructor. Of course, for objects which do
991  * not have mutable data, a hit arises by returning by value in cases
992  * where it is not intended to initialize a local variable at all nor
993  * to cancel a thread: where, say, only const methods are to be called
994  * on the return value (which could be done directly if this method
995  * returned by const reference). However, in many use cases this will
996  * be mitigated by the move_get() method.
997  */
998  Val get();
999 
1000 /**
1001  * Gets the stored value obtained from the function which is
1002  * represented by this object by a move operation, if the return type
1003  * implements a move constructor (otherwise this method does the same
1004  * as the get() method). It is provided as an option for cases where
1005  * a move is required for efficiency reasons, but although it may be
1006  * called by any thread, a move from this Thread::Future object may
1007  * normally only be made once (except where the return type has been
1008  * designed to be moved more than once for the limited purpose of
1009  * inspecting a flag indicating whether its value is valid or not).
1010  * If this method is to be called then no calls to get() by another
1011  * thread should normally be made and no calls to when() should be
1012  * made. If the worker thread launched by the call to run() has not
1013  * completed, then this method will block until it has completed. If
1014  * run() has not been called, then run() will be called (and this
1015  * method will block until the launched worker thread completes). If
1016  * the function which is represented by this object throws
1017  * Cgu::Thread::Exit or an uncaught exception derived from
1018  * std::exception, then the exception will have been consumed by this
1019  * Cgu::Thread::Future object and the error flag will have been set.
1020  * The error flag will also have been set if the worker thread is
1021  * cancelled or the thread wrapper in this Cgu::Thread::Future object
1022  * threw std::bad_alloc. This method is a cancellation point if it
1023  * blocks, and is cancellation safe if the stack unwinds on
1024  * cancellation. This method is only exception safe if the return
1025  * type's move constructor is exception safe.
1026  * @return The value obtained from the function which is represented
1027  * by this object
1028  * @exception Cgu::Thread::FutureThreadError This method might throw
1029  * Cgu::Thread::FutureThreadError if run() has not previously been
1030  * called and the thread did not start properly when this function
1031  * called run().
1032  * @exception std::bad_alloc This method might throw std::bad_alloc if
1033  * run() has not previously been called, memory is exhausted and the
1034  * system throws in that case. (This exception will not be thrown if
1035  * the library has been installed using the
1036  * --with-glib-memory-slices-no-compat configuration option: instead
1037  * glib will terminate the program if it is unable to obtain memory
1038  * from the operating system.)
1039  * @note 1. This method might also throw if the copy or move
1040  * constructor of the returned value type throws.
1041  * @note 2. Question: Couldn't this method return the stored value by
1042  * rvalue reference? Answer: It could. However, because of return
1043  * value optimization, which will be implemented by any compiler
1044  * capable of compiling this library, no advantage would be gained by
1045  * doing so when initializing a local variable with the return value
1046  * of this method (the move constructor will only be called once, and
1047  * no call will be made to the copy constructor, whether returning by
1048  * value or rvalue reference). The advantage of returning by value is
1049  * that the call to the move constructor is forced to be within this
1050  * Thread::Future object's mutex, so different threads' calls to the
1051  * move constructor are serialized, and also with blocked
1052  * cancellation, so this method is cancellation safe. All calls to
1053  * this method by different threads are therefore isolated and we do
1054  * not have to worry about the thread safety of the mutating first
1055  * call to this method, nor about direct access to the stored value
1056  * via a rvalue reference outside the mutex nor the cancellation
1057  * safety of the move constructor.
1058  *
1059  * Since 2.0.11
1060  */
1061  Val move_get();
1062 
1063 /**
1064  * Cancels the worker thread in which the function represented by this
1065  * object runs, if that function has not yet finished. If this method
1066  * is called and the worker thread is still running and is cancelled
1067  * in response to a call to this method, then the error flag will be
1068  * set so that a method calling get() or move_get() can examine
1069  * whether the result is valid. If run() has not yet been called or
1070  * the worker thread has already finished executing the function
1071  * represented by this object then this function does nothing and
1072  * returns false. This method is thread safe and may be called by any
1073  * thread. It will not throw.
1074  * @return true if run() has previously been called and the worker
1075  * thread has not yet finished executing the function represented by
1076  * this object, otherwise false (in which case this method does
1077  * nothing).
1078  * @note 1. Use this method with care. When cancelling a thread not
1079  * all thread implementations will unwind the stack, and so run the
1080  * destructors of local objects. This is discussed further in the
1081  * documentation on Cgu::Thread::Thread::cancel().
1082  * @note 2. This method might return true because the worker thread
1083  * has not yet finished, but the error flag might still not be set.
1084  * This is because the worker thread may not meet a cancellation point
1085  * before it ends naturally. It is the error flag which indicates
1086  * definitively whether the worker thread terminated prematurely in
1087  * response to a call to this method.
1088  */
1089  bool cancel();
1090 
1091 /**
1092  * A utility enabling the value returned by the thread function
1093  * represented by this Cgu::Thread::Future object to be dealt with
1094  * asynchronously rather than by (or in addition to) a call to the
1095  * get() method. It causes the callback passed as an argument to this
1096  * method (referred to below as the 'when' callback) to be executed by
1097  * a thread's main loop if and when the thread function represented by
1098  * this Cgu::Thread::Future object finishes correctly - the 'when'
1099  * callback is passed that thread function's return value when it is
1100  * invoked. This method is thread safe, and may be called by any
1101  * thread.
1102  *
1103  * This functionality is implemented by connecting an internal
1104  * dispatching callback to the done_emitter object.
1105  *
1106  * The 'when' callback should take a single unbound argument
1107  * comprising a const reference to the return type of the thread
1108  * function represented by this Cgu::Thread::Future object. (So, in
1109  * the case of a Future<int> object, the callback function should take
1110  * a const int& argument as the unbound argument.) The 'when'
1111  * callback can have any number of bound arguments, except that a
1112  * bound argument may not include a copy of this Cgu::Thread::Future
1113  * object held by intrusive pointer as returned by the make() methods
1114  * (that would result in this Cgu::Thread::Future object owning, via
1115  * done_emitter, a reference to itself and so become incapable of
1116  * being freed). The 'when' callback may, however, take a pointer to
1117  * this Cgu::Thread::Future object, as obtained by the
1118  * Cgu::IntrusivePtr::get() method, because this Cgu::Thread::Future
1119  * object is guaranteed to remain in existence until the callback has
1120  * completed executing.
1121  *
1122  * This method cannot be called after the thread function represented
1123  * by this Cgu::Thread::Future object has completed (either
1124  * successfully or unsuccessfully) so that is_done() would return
1125  * true, and if this is attempted a Cgu::Thread::FutureWhenError
1126  * exception will be thrown. Therefore, generally this method should
1127  * be called before the run() method has been called.
1128  *
1129  * Once the run() method has been called, this Cgu::Thread::Future
1130  * object will always stay in existence until the thread function
1131  * represented by it has completed (whether correctly, by cancellation
1132  * or by a thrown exception), and any 'when' callback (and any other
1133  * callbacks connected to the done_emitter object) and any 'fail'
1134  * callback have completed. Accordingly it is safe to use this method
1135  * even if the intrusive pointer object returned by the make() methods
1136  * will go out of scope before the 'when' callback has executed: the
1137  * callback will execute correctly irrespective of that.
1138  *
1139  * Summary: use of this method is safe and has been implemented in a
1140  * way which does not give rise to timing issues.
1141  *
1142  * If memory is exhausted and std::bad_alloc is thrown by the thread
1143  * wrapper of Cgu::Thread::Future after run() is called or by
1144  * done_emitter when emitting, or if the thread function represented
1145  * by this Cgu::Thread::Future object throws Cgu::Thread::Exit, exits
1146  * with an uncaught exception deriving from std::exception or is
1147  * cancelled, or if the copy constructor of a non-reference argument
1148  * of the function represented by the 'when' callback throws, or any
1149  * other callback has been connected to done_emitter before this
1150  * method is called which exits with an uncaught exception, then the
1151  * 'when' callback will not execute (instead the exception concerned
1152  * will be consumed and an error indicated). With many systems, swap
1153  * memory combined with memory over-commit makes it pointless to check
1154  * for std::bad_alloc (and even more so in programs using glib, as
1155  * glib aborts a program where it cannot obtain memory from the
1156  * operating system). So subject to that, if the user program is
1157  * designed so that the thread function represented by this
1158  * Cgu::Thread::Future object does not exit with uncaught exceptions,
1159  * does not throw Cgu::Thread::Exit and is not cancelled, and so that
1160  * the 'when' callback does not exit with an uncaught exception (and
1161  * the function represented by that callback either takes no arguments
1162  * of class type by value or the copy constructors of any of its value
1163  * arguments do not throw), and if this method is called before any
1164  * other callbacks are connected to done_emitter, the possibility of
1165  * failure can be disregarded.
1166  *
1167  * In cases where that is not true and detecting whether a failure has
1168  * occurred is required, a fail() method is provided. It should be
1169  * noted that a callback handed to the fail() method will not execute
1170  * in a case of error if the error comprises the 'when' callback
1171  * exiting with an uncaught exception when it is executed by the main
1172  * loop, or the copy constructor of any value argument of the function
1173  * represented by the 'when' callback throwing (such exceptions would
1174  * be consumed internally in order to protect the main loop and a
1175  * g_critical message issued). If the 'when' callback might exit with
1176  * an uncaught exception when executing or have the copy constructor
1177  * of a value argument throw, and doing something other than consuming
1178  * the exception and issuing a g_critical message is required, then a
1179  * different approach is to start a new thread to wait on the get()
1180  * method which can act on the result of is_error() directly.
1181  *
1182  * If glib < 2.32 is used, the glib main loop must have been made
1183  * thread-safe by a call to g_thread_init() before this function is
1184  * called. glib >= 2.32 does not require g_thread_init() to be called
1185  * in order to be thread safe.
1186  *
1187  * @param cb The 'when' callback (the callback to be executed when the
1188  * function represented by this Cgu::Thread::Future object has
1189  * successfully completed). Ownership is taken of this object, and it
1190  * will be deleted when it has been finished with.
1191  * @param priority The priority to be given to the 'when' callback in
1192  * the main loop after the thread function represented by this
1193  * Cgu::Thread::Future object has successfully completed. In
1194  * ascending order of priorities, priorities are G_PRIORITY_LOW,
1195  * G_PRIORITY_DEFAULT_IDLE, G_PRIORITY_HIGH_IDLE, G_PRIORITY_DEFAULT
1196  * and G_PRIORITY_HIGH. The default is G_PRIORITY_DEFAULT. This
1197  * determines the order in which the callback will appear in the event
1198  * list in the main loop, not the priority which the OS will adopt.
1199  * @param context The glib main context of the thread in whose main
1200  * loop the 'when' callback is to be executed (the default of NULL
1201  * will cause the callback to be executed in the main program loop).
1202  * @return The internal dispatching callback created by this method
1203  * and connected to done_emitter. It is made available as a return
1204  * value so that if wanted it can be disconnected programmatically
1205  * from done_emitter, or block()/unblock() can be called on it (but if
1206  * that is to be done, it must be done before the thread function
1207  * represented by this Cgu::Thread::Future object has completed in
1208  * order for it to be effective).
1209  * @exception Cgu::Thread::FutureWhenError This method will throw
1210  * Cgu::Thread::FutureWhenError if it is called after the thread
1211  * function represented by this Cgu::Thread::Future object has
1212  * completed. If it does so, the 'when' callback will be disposed of.
1213  * @exception std::bad_alloc This method might throw std::bad_alloc if
1214  * memory is exhausted and the system throws in that case. If it does
1215  * so, the 'when' callback will be disposed of.
1216  * @note The return value of the function represented by this
1217  * Cgu::Thread::Future object is stored and passed as an argument to
1218  * the 'when' callback by const reference. If other threads might
1219  * concurrently call this object's get() method, which copies the
1220  * stored value, the stored type's copy constructor must be thread
1221  * safe with respect to the stored type's const methods. This would
1222  * be relevant if the stored type has data members declared mutable
1223  * which would be copied by its copy constructor.
1224  *
1225  * Since 2.0.2
1226  */
1228  gint priority = G_PRIORITY_DEFAULT,
1229  GMainContext* context = 0);
1230 
1231 /**
1232  * This is a version of the utility enabling the value returned by the
1233  * thread function represented by this Cgu::Thread::Future object to
1234  * be dealt with asynchronously, which takes a Releaser object for
1235  * automatic disconnection of the callback passed as an argument to
1236  * this method (referred to below as the 'when' callback), if the
1237  * object having the 'when' callback function as a member is
1238  * destroyed. For this to be race free, the lifetime of that object
1239  * must be controlled by the thread in whose main loop the 'when'
1240  * callback will execute.
1241  *
1242  * If the 'when' callback has not been released, this method causes it
1243  * to be executed by a thread's main loop if and when the thread
1244  * function represented by this Cgu::Thread::Future object finishes
1245  * correctly - the 'when' callback is passed that thread function's
1246  * return value when it is invoked. This method is thread safe, and
1247  * may be called by any thread.
1248  *
1249  * This functionality is implemented by connecting an internal
1250  * dispatching callback to the done_emitter object.
1251  *
1252  * The 'when' callback should take a single unbound argument
1253  * comprising a const reference to the return type of the thread
1254  * function represented by this Cgu::Thread::Future object. (So, in
1255  * the case of a Future<int> object, the callback function should take
1256  * a const int& argument as the unbound argument.) The 'when'
1257  * callback can have any number of bound arguments, except that a
1258  * bound argument may not include a copy of this Cgu::Thread::Future
1259  * object held by intrusive pointer as returned by the make() methods
1260  * (that would result in this Cgu::Thread::Future object owning, via
1261  * done_emitter, a reference to itself and so become incapable of
1262  * being freed). The 'when' callback may, however, take a pointer to
1263  * this Cgu::Thread::Future object, as obtained by the
1264  * Cgu::IntrusivePtr::get() method, because this Cgu::Thread::Future
1265  * object is guaranteed to remain in existence until the callback has
1266  * completed executing.
1267  *
1268  * This method cannot be called after the thread function represented
1269  * by this Cgu::Thread::Future object has completed (either
1270  * successfully or unsuccessfully) so that is_done() would return
1271  * true, and if this is attempted a Cgu::Thread::FutureWhenError
1272  * exception will be thrown. Therefore, generally this method should
1273  * be called before the run() method has been called.
1274  *
1275  * The documentation for the version of this method which does not
1276  * take a Releaser object gives further details of how this method is
1277  * used.
1278  *
1279  * If glib < 2.32 is used, the glib main loop must have been made
1280  * thread-safe by a call to g_thread_init() before this function is
1281  * called. glib >= 2.32 does not require g_thread_init() to be called
1282  * in order to be thread safe.
1283  *
1284  * @param cb The 'when' callback (the callback to be executed when the
1285  * function represented by this Cgu::Thread::Future object has
1286  * successfully completed). Ownership is taken of this object, and it
1287  * will be deleted when it has been finished with.
1288  * @param r A Releaser object for automatic disconnection of the
1289  * 'when' callback if the object of which the callback function is a
1290  * member is destroyed.
1291  * @param priority The priority to be given to the 'when' callback in
1292  * the main loop after the thread function represented by this
1293  * Cgu::Thread::Future object has successfully completed. In
1294  * ascending order of priorities, priorities are G_PRIORITY_LOW,
1295  * G_PRIORITY_DEFAULT_IDLE, G_PRIORITY_HIGH_IDLE, G_PRIORITY_DEFAULT
1296  * and G_PRIORITY_HIGH. The default is G_PRIORITY_DEFAULT. This
1297  * determines the order in which the callback will appear in the event
1298  * list in the main loop, not the priority which the OS will adopt.
1299  * @param context The glib main context of the thread in whose main
1300  * loop the 'when' callback is to be executed (the default of NULL
1301  * will cause the callback to be executed in the main program loop).
1302  * @return The internal dispatching callback created by this method
1303  * and connected to done_emitter. It is made available as a return
1304  * value so that if wanted it can be disconnected programmatically
1305  * from done_emitter, or block()/unblock() can be called on it (but if
1306  * that is to be done, it must be done before the thread function
1307  * represented by this Cgu::Thread::Future object has completed in
1308  * order for it to be effective).
1309  * @exception Cgu::Thread::FutureWhenError This method will throw
1310  * Cgu::Thread::FutureWhenError if it is called after the thread
1311  * function represented by this Cgu::Thread::Future object has
1312  * completed. If it does so, the 'when' callback will be disposed of.
1313  * @exception std::bad_alloc This method might throw std::bad_alloc if
1314  * memory is exhausted and the system throws in that case. If it does
1315  * so, the 'when' callback will be disposed of.
1316  * @exception Cgu::Thread::MutexError This method will throw
1317  * Cgu:Thread::MutexError if initialisation of the mutex in a
1318  * SafeEmitterArg object constructed by this method fails. If it does
1319  * so, the 'when' callback will be disposed of. (It is often not
1320  * worth checking for this, as it means either memory is exhausted or
1321  * pthread has run out of other resources to create new mutexes.)
1322  * @note 1. The return value of the function represented by this
1323  * Cgu::Thread::Future object is stored and passed as an argument to
1324  * the 'when' callback by const reference. If other threads might
1325  * concurrently call this object's get() method, which copies the
1326  * stored value, the stored type's copy constructor must be thread
1327  * safe with respect to the stored type's const methods. This would
1328  * be relevant if the stored type has data members declared mutable
1329  * which would be copied by its copy constructor.
1330  * @note 2. By virtue of the Releaser object, it is in theory possible
1331  * (if memory is exhausted and the system throws in that case) that an
1332  * internal SafeEmitterArg object will throw std::bad_alloc when
1333  * emitting/executing the 'when' callback in the glib main loop, with
1334  * the result that the relevant callback will not execute (instead the
1335  * exception will be consumed and a g_critical() warning will be
1336  * issued). This is rarely of any relevance because glib will abort
1337  * the program if it is itself unable to obtain memory from the
1338  * operating system. However, where it is relevant, design the
1339  * program so that it is not necessary to provide a releaser object.
1340  *
1341  * Since 2.0.2
1342  */
1344  Cgu::Releaser& r,
1345  gint priority = G_PRIORITY_DEFAULT,
1346  GMainContext* context = 0);
1347 
1348 /**
1349  * A utility intended to be used where relevant in conjunction with
1350  * the when() methods. It enables a callback to be executed in a glib
1351  * main loop (referred to below as the 'fail' callback) if memory is
1352  * exhausted and std::bad_alloc was thrown by the thread wrapper of
1353  * Cgu::Thread::Future after calling run() or by done_emitter when
1354  * emitting, or if the thread function represented by this
1355  * Cgu::Thread::Future object threw Cgu::Thread::Exit, exited with an
1356  * uncaught exception deriving from std::exception or was cancelled
1357  * (or that function took an argument of class type by value whose
1358  * copy constructor threw such an exception), or any callback
1359  * connected to done_emitter exited with an uncaught exception. It
1360  * therefore enables errors to be detected and acted on without having
1361  * a thread wait on the get() method in order to test is_error() or
1362  * is_emitter_error().
1363  *
1364  * It is implemented by attaching a timeout to the main loop which
1365  * polls at 100 millisecond intervals and tests is_done()/is_error()
1366  * and is_emitter_done()/is_emitter_error(). The timeout is
1367  * automatically removed by the implementation once it has been
1368  * detected that an error has occurred and the 'fail' callback is
1369  * executed, or if the thread function represented by this Cgu::Future
1370  * object and all done_emitter emissions (including execution of any
1371  * 'when' callback) have completed successfully.
1372  *
1373  * This method can be called before or after the run() method has been
1374  * called, and whether or not the thread function represented by this
1375  * Cgu::Thread::Future object has completed.
1376  *
1377  * Once this method has been called, this Cgu::Thread::Future object
1378  * will always stay in existence until the timeout has been
1379  * automatically removed by the implementation. Accordingly it is
1380  * safe to use this method even if the intrusive pointer object
1381  * returned by the make() methods will go out of scope before the
1382  * 'fail' callback has executed: the callback will execute correctly
1383  * irrespective of that.
1384  *
1385  * This method does not have a priority argument: as a polling timeout
1386  * is created, a particular priority will normally have no
1387  * significance (in fact, the 'fail' callback will execute in the main
1388  * loop with a priority of G_PRIORITY_DEFAULT). If in a special case
1389  * a different polling interval than 100 milliseconds or a different
1390  * priority is required, users can attach their own polling timeouts
1391  * to a main loop and carry out the tests by hand.
1392  *
1393  * Four other points should be noted. First, if as well as the when()
1394  * method being called some other callback has been connected to
1395  * done_emitter, and that other callback throws, the 'fail' callback
1396  * will execute. Therefore, if the particular program design requires
1397  * that the 'fail' callback should only execute if the 'when' callback
1398  * is not executed (and the 'when' callback only execute if the 'fail'
1399  * callback does not execute), no other callbacks which throw should
1400  * be connected to done_emitter.
1401  *
1402  * Secondly, as mentioned in the documentation on the when() method,
1403  * if the 'when' callback exits with an uncaught exception upon being
1404  * executed by the main loop or the function it represents takes an
1405  * argument by value whose copy constructor throws, the 'fail'
1406  * callback will not execute (the exception will have been consumed
1407  * internally in order to protect the main loop and a g_critical
1408  * message issued).
1409  *
1410  * Thirdly, avoid if possible having the 'fail' callback represent a
1411  * function which either might throw or takes an argument by value
1412  * whose copy constructor might throw (such an exception would be
1413  * consumed internally in order to protect the main loop and a
1414  * g_critical message issued, but no other error indication apart from
1415  * the g_critical message will be provided).
1416  *
1417  * Fourthly, unlike the 'when' callback, a copy of this
1418  * Cgu::Thread::Future object held by intrusive pointer as returned by
1419  * the make() methods may safely be bound to the 'fail' callback,
1420  * which would enable the 'fail' callback to determine whether it is
1421  * is_error() or is_emitter_error() which returns false.
1422  *
1423  * If glib < 2.32 is used, the glib main loop must have been made
1424  * thread-safe by a call to g_thread_init() before this function is
1425  * called. glib >= 2.32 does not require g_thread_init() to be called
1426  * in order to be thread safe.
1427  *
1428  * @param cb The 'fail' callback (the callback to be executed if the
1429  * thread function represented by this Cgu::Thread::Future object or a
1430  * done_emitter emission has failed to complete). Ownership is taken
1431  * of this object, and it will be deleted when it has been finished
1432  * with.
1433  * @param context The glib main context of the thread in whose main
1434  * loop the 'fail' callback is to be executed (the default of NULL
1435  * will cause the functor to be executed in the main program loop).
1436  * @exception std::bad_alloc This method might throw std::bad_alloc if
1437  * memory is exhausted and the system throws in that case. If it does
1438  * so, the 'fail' callback will be disposed of.
1439  *
1440  * Since 2.0.2
1441  */
1442  void fail(const Cgu::Callback::Callback* cb,
1443  GMainContext* context = 0);
1444 
1445 /**
1446  * This is a version of the fail() utility for use in conjunction with
1447  * the when() methods, which takes a Releaser object for automatic
1448  * disconnection of the callback functor passed as an argument to this
1449  * method if the object having the callback function as a member is
1450  * destroyed. For this to be race free, the lifetime of that object
1451  * must be controlled by the thread in whose main loop the 'fail'
1452  * callback will execute.
1453  *
1454  * This method enables a callback to be executed in a glib main loop
1455  * if memory is exhausted and std::bad_alloc was thrown by the thread
1456  * wrapper of Cgu::Thread::Future after calling run() or by
1457  * done_emitter when emitting, or if the thread function represented
1458  * by this Cgu::Thread::Future object threw Cgu::Thread::Exit, exited
1459  * with an uncaught exception deriving from std::exception or was
1460  * cancelled (or that function took an argument of class type by value
1461  * whose copy constructor threw such an exception), or any callback
1462  * connected to done_emitter exited with an uncaught exception. It
1463  * therefore enables errors to be detected and acted on without having
1464  * a thread wait on the get() method in order to test is_error() or
1465  * is_emitter_error().
1466  *
1467  * This method can be called before or after the run() method has been
1468  * called, and whether or not the thread function represented by this
1469  * Cgu::Thread::Future object has completed.
1470  *
1471  * The documentation for the version of this method which does not
1472  * take a Releaser object gives further details of how this method is
1473  * used.
1474  *
1475  * If glib < 2.32 is used, the glib main loop must have been made
1476  * thread-safe by a call to g_thread_init() before this function is
1477  * called. glib >= 2.32 does not require g_thread_init() to be called
1478  * in order to be thread safe.
1479  *
1480  * @param cb The 'fail' callback (the callback to be executed if the
1481  * thread function represented by this Cgu::Thread::Future object or a
1482  * done_emitter emission has failed to complete). Ownership is taken
1483  * of this object, and it will be deleted when it has been finished
1484  * with.
1485  * @param r A Releaser object for automatic disconnection of the
1486  * 'fail' callback if the object of which the callback function is a
1487  * member is destroyed.
1488  * @param context The glib main context of the thread in whose main
1489  * loop the 'fail' callback is to be executed (the default of NULL
1490  * will cause the functor to be executed in the main program loop).
1491  * @exception std::bad_alloc This method might throw std::bad_alloc if
1492  * memory is exhausted and the system throws in that case. If it does
1493  * so, the 'fail' callback will be disposed of.
1494  * @exception Cgu::Thread::MutexError This method will throw
1495  * Cgu:Thread::MutexError if initialisation of the mutex in a
1496  * SafeEmitterArg object constructed by Cgu::start_timeout() fails.
1497  * If it does so, the 'fail' callback will be disposed of. (It is
1498  * often not worth checking for this, as it means either memory is
1499  * exhausted or pthread has run out of other resources to create new
1500  * mutexes.)
1501  * @note By virtue of the Releaser object, it is in theory possible
1502  * (if memory is exhausted and the system throws in that case) that an
1503  * internal SafeEmitterArg object will throw std::bad_alloc when
1504  * emitting/executing the 'fail' callback in the glib main loop, with
1505  * the result that the relevant callback will not execute (instead the
1506  * exception will be consumed and a g_critical() warning will be
1507  * issued). This is rarely of any relevance because glib will abort
1508  * the program if it is itself unable to obtain memory from the
1509  * operating system. However, where it is relevant, design the
1510  * program so that it is not necessary to provide a releaser object.
1511  *
1512  * Since 2.0.2
1513  */
1514  void fail(const Cgu::Callback::Callback* cb,
1515  Cgu::Releaser& r,
1516  GMainContext* context = 0);
1517 
1518 /**
1519  * @return true if the function represented by this
1520  * Cgu::Thread::Future object has finished, either by returning
1521  * normally, by cancellation or by virtue of having thrown
1522  * Cgu::Thread::Exit or some exception derived from std::exception.
1523  * Once this method returns true, then it is guaranteed that the get()
1524  * or move_get() method will not block (except as incidental to any
1525  * contention between threads calling get()). Once this method has
1526  * returned true or get() or move_get() has unblocked, then the result
1527  * of is_error() is definitive. This method is thread safe and may be
1528  * called by any thread. It will not throw.
1529  * @note This method will return true even though any callbacks
1530  * connected to done_emitter are still executing or waiting to
1531  * execute. From version 2.0.2 the is_emitter_done() method will
1532  * indicate when done_emitter callbacks (if any) have also completed.
1533  */
1534  bool is_done() const;
1535 
1536 /**
1537  * @return true if both the function represented by this
1538  * Cgu::Thread::Future object has finished and any callbacks connected
1539  * to done_emitter have completed. Once this method returns true,
1540  * then the result of is_emitter_error() is definitive. This method
1541  * is thread safe and may be called by any thread. It will not throw.
1542  * @note This method will return true automatically if is_error() and
1543  * is_done() return true, because if the function represented by this
1544  * Cgu::Thread::Future object was cancelled or exited with an uncaught
1545  * exception, done_emitter is never emitted. In addition, if this
1546  * method returns true, then is_done() must also return true.
1547  *
1548  * Since 2.0.2
1549  */
1550  bool is_emitter_done() const;
1551 
1552 /**
1553  * @return true if (a) a Cgu::Thread::Exit exception has been thrown
1554  * by the function represented by this Cgu::Thread::Future object
1555  * (which will have been consumed by this Cgu::Thread::Future object),
1556  * (b) an exception derived from std::exception has been thrown by
1557  * that function which was not caught in that function (and which will
1558  * also have been consumed by this Cgu::Thread::Future object), (c)
1559  * the worker thread in which that function runs was cancelled in
1560  * mid-course with a call to cancel() or (d) the thread wrapper
1561  * implementing the worker thread in this Cgu::Thread::Future object
1562  * threw and then consumed std::bad_alloc (this is different from the
1563  * run() method throwing std::bad_alloc). In these cases the value
1564  * obtained by get() or move_get() will not be valid (it will be a
1565  * default constructed object of the return type of the function
1566  * represented by this Cgu::Thread::Future object). Otherwise this
1567  * method returns false. The result of this method is definitive once
1568  * get() or move_get() has unblocked or is_done() returns true. This
1569  * method is thread safe and may be called by any thread. It will not
1570  * throw.
1571  */
1572  bool is_error() const;
1573 
1574 /**
1575  * @return true if an uncaught exception arose in emitting @ref
1576  * DoneEmitterAnchor "done_emitter" when executing callbacks connected
1577  * to it. Otherwise this method returns false. The result of this
1578  * method is definitive once is_emitter_done() returns true. This
1579  * method is thread safe and may be called by any thread. It will not
1580  * throw.
1581  * @note This method will return false automatically if is_error()
1582  * returns true, because if the function represented by this
1583  * Cgu::Thread::Future object was cancelled or exited with an uncaught
1584  * exception, done_emitter is never emitted. It follows that if this
1585  * method returns true, is_error() must return false.
1586  */
1587  bool is_emitter_error() const;
1588 
1589 /**
1590  * A Cgu::SafeEmitter object which is emitted when the function
1591  * represented by this Cgu::Thread::Future object finishes correctly
1592  * (that is, the function is not cancelled and does not throw any
1593  * uncaught exceptions). By itself it does not do too much as it is
1594  * emitted (and connected callbacks execute in) the same worker thread
1595  * immediately after the Future function has completed. However, any
1596  * thread can connect a Callback object to this Cgu::SafeEmitter
1597  * object and a connected callback can, say, cause another Callback to
1598  * be executed in a thread's main loop using Cgu::Callback::post(),
1599  * and from version 2.0.2 when() methods are provided which will do
1600  * this for users automatically. Once the run() method has been
1601  * called, this Cgu::Thread::Future object (and so done_emitter) will
1602  * always stay in existence until the function represented by it has
1603  * completed (whether correctly, by cancellation or by a thrown
1604  * exception) and any callbacks connected to the done_emitter object
1605  * have completed, irrespective of whether the intrusive pointer
1606  * returned by the make() methods has gone out of scope.
1607  * @note 1. Cancellation is blocked while the Cgu::SafeEmitter object
1608  * emits and any connected callback executes.
1609  * @note 2. A connected callback can however terminate the worker
1610  * thread by throwing Cgu::Thread::Exit (in which case no subsequent
1611  * callbacks to be executed on that emission will execute either: the
1612  * worker thread will safely terminate and unwind the stack in so
1613  * doing). In that event, the emitter_error flag will be set.
1614  * @note 3. All other uncaught exceptions which might be thrown by the
1615  * Cgu::SafeEmitter object emitting, or by a connected callback
1616  * function executing, are consumed to retain the integrity of the
1617  * Thread::Future object. In the event of such an exception being
1618  * thrown, the emitter_error flag will be set. In summary, the
1619  * emitter_error flag will be set if (a) a callback function throws
1620  * Cgu::Thread::Exit, (b) some other uncaught exception escapes from a
1621  * callback function or (c) Cgu::SafeEmitter::emit() throws
1622  * std::bad_alloc or the copy constructor of a bound argument which is
1623  * not a reference argument has thrown. If the user knows that the
1624  * callback function does not throw Cgu::Thread::Exit and does not
1625  * allow any other exception to escape, then the cause must be a
1626  * std::bad_alloc memory exception in Cgu::SafeEmitter::emit() or the
1627  * copy constructor of a non-reference bound argument throwing.
1628  * @note 4. An emission is thread safe if the connected callback
1629  * functions are thread safe.
1630  * @note 5. This Cgu::Thread::Future object's mutex is released while
1631  * the Cgu::SafeEmitter object emits. This means that any connected
1632  * callbacks can safely call, say, the Future object's get() or
1633  * is_error() methods. However, a connected callback should not have
1634  * a bound argument comprising a copy of this Cgu::Thread::Future
1635  * object held by intrusive pointer as returned by the make() methods
1636  * (that would result in this Cgu::Thread::Future object owning, via
1637  * done_emitter, a reference to itself and so become incapable of
1638  * being freed). The callback may, however, take a pointer to this
1639  * Cgu::Thread::Future object as a bound argument, as obtained by the
1640  * Cgu::IntrusivePtr::get() method, because this Cgu::Thread::Future
1641  * object is guaranteed to remain in existence until all callbacks
1642  * connected to done_emitter have completed executing.
1643  * @anchor DoneEmitterAnchor
1644  */
1646 
1647 /* Only has effect if --with-glib-memory-slices-compat or
1648  * --with-glib-memory-slices-no-compat option picked */
1650 };
1651 
1652 /**
1653  * A convenience helper function which calls
1654  * Cgu::Thread::Future::make() to obtain a Future object without the
1655  * need to specify the return value of the function represented by the
1656  * new object: that is deduced from the signature of that function.
1657  * This is useful shorthand when also employed with the C++11 'auto'
1658  * keyword.
1659  * @exception std::bad_alloc It might throw std::bad_alloc if memory
1660  * is exhausted and the system throws in that case. (This exception
1661  * will not be thrown if the library has been installed using the
1662  * --with-glib-memory-slices-no-compat configuration option: instead
1663  * glib will terminate the program if it is unable to obtain memory
1664  * from the operating system.)
1665  * @exception Cgu::Thread::MutexError It might throw
1666  * Cgu::Thread::MutexError if initialisation of the contained mutex
1667  * fails. (It is often not worth checking for this, as it means
1668  * either memory is exhausted or pthread has run out of other
1669  * resources to create new mutexes.)
1670  * @exception Cgu::Thread::CondError It might throw
1671  * Cgu::Thread::CondError if initialisation of the contained condition
1672  * variable fails. (It is often not worth checking for this, as it
1673  * means either memory is exhausted or pthread has run out of other
1674  * resources to create new condition variables.)
1675  * @note This method will also throw if the copy or move constructor
1676  * of a bound argument throws, or the default constructor of the
1677  * return value type of the function represented by the new object
1678  * throws.
1679 
1680  *
1681  * Since 2.0.4
1682  */
1683 template <class Obj, class Ret, class... Params, class... Args>
1685  Ret (Obj::*func)(Params...),
1686  Args&&... args) {
1687  return Cgu::Thread::Future<Ret>::make(obj, func, std::forward<Args>(args)...);
1688 }
1689 
1690 /**
1691  * A convenience helper function which calls
1692  * Cgu::Thread::Future::make() to obtain a Future object without the
1693  * need to specify the return value of the function represented by the
1694  * new object: that is deduced from the signature of that function.
1695  * This is useful shorthand when also employed with the C++11 'auto'
1696  * keyword.
1697  * @exception std::bad_alloc It might throw std::bad_alloc if memory
1698  * is exhausted and the system throws in that case. (This exception
1699  * will not be thrown if the library has been installed using the
1700  * --with-glib-memory-slices-no-compat configuration option: instead
1701  * glib will terminate the program if it is unable to obtain memory
1702  * from the operating system.)
1703  * @exception Cgu::Thread::MutexError It might throw
1704  * Cgu::Thread::MutexError if initialisation of the contained mutex
1705  * fails. (It is often not worth checking for this, as it means
1706  * either memory is exhausted or pthread has run out of other
1707  * resources to create new mutexes.)
1708  * @exception Cgu::Thread::CondError It might throw
1709  * Cgu::Thread::CondError if initialisation of the contained condition
1710  * variable fails. (It is often not worth checking for this, as it
1711  * means either memory is exhausted or pthread has run out of other
1712  * resources to create new condition variables.)
1713  * @note This method will also throw if the copy or move constructor
1714  * of a bound argument throws, or the default constructor of the
1715  * return value type of the function represented by the new object
1716  * throws.
1717  *
1718  * Since 2.0.4
1719  */
1720 template <class Obj, class Ret, class... Params, class... Args>
1722  Ret (Obj::*func)(Params...) const,
1723  Args&&... args) {
1724  return Cgu::Thread::Future<Ret>::make(obj, func, std::forward<Args>(args)...);
1725 }
1726 
1727 /**
1728  * A convenience helper function which calls
1729  * Cgu::Thread::Future::make() to obtain a Future object without the
1730  * need to specify the return value of the function represented by the
1731  * new object: that is deduced from the signature of that function.
1732  * This is useful shorthand when also employed with the C++11 'auto'
1733  * keyword.
1734  * @exception std::bad_alloc It might throw std::bad_alloc if memory
1735  * is exhausted and the system throws in that case. (This exception
1736  * will not be thrown if the library has been installed using the
1737  * --with-glib-memory-slices-no-compat configuration option: instead
1738  * glib will terminate the program if it is unable to obtain memory
1739  * from the operating system.)
1740  * @exception Cgu::Thread::MutexError It might throw
1741  * Cgu::Thread::MutexError if initialisation of the contained mutex
1742  * fails. (It is often not worth checking for this, as it means
1743  * either memory is exhausted or pthread has run out of other
1744  * resources to create new mutexes.)
1745  * @exception Cgu::Thread::CondError It might throw
1746  * Cgu::Thread::CondError if initialisation of the contained condition
1747  * variable fails. (It is often not worth checking for this, as it
1748  * means either memory is exhausted or pthread has run out of other
1749  * resources to create new condition variables.)
1750  * @note This method will also throw if the copy or move constructor
1751  * of a bound argument throws, or the default constructor of the
1752  * return value type of the function represented by the new object
1753  * throws.
1754  *
1755  * Since 2.0.4
1756  */
1757 template <class Ret, class... Params, class... Args>
1759  Args&&... args) {
1760  return Cgu::Thread::Future<Ret>::make(func, std::forward<Args>(args)...);
1761 }
1762 
1763 /**
1764  * A convenience helper function which calls
1765  * Cgu::Thread::Future::make() to obtain a Future object without the
1766  * need to specify the return value of the function represented by the
1767  * new object: that is deduced from the signature of that function.
1768  * This is useful shorthand when also employed with the C++11 'auto'
1769  * keyword.
1770  * @exception std::bad_alloc It might throw std::bad_alloc if memory
1771  * is exhausted and the system throws in that case. (This exception
1772  * will not be thrown if the library has been installed using the
1773  * --with-glib-memory-slices-no-compat configuration option: instead
1774  * glib will terminate the program if it is unable to obtain memory
1775  * from the operating system.)
1776  * @exception Cgu::Thread::MutexError It might throw
1777  * Cgu::Thread::MutexError if initialisation of the contained mutex
1778  * fails. (It is often not worth checking for this, as it means
1779  * either memory is exhausted or pthread has run out of other
1780  * resources to create new mutexes.)
1781  * @exception Cgu::Thread::CondError It might throw
1782  * Cgu::Thread::CondError if initialisation of the contained condition
1783  * variable fails. (It is often not worth checking for this, as it
1784  * means either memory is exhausted or pthread has run out of other
1785  * resources to create new condition variables.)
1786  * @note This method will also throw if the copy or move constructor
1787  * of a bound argument throws, or the default constructor of the
1788  * return value type of the function represented by the new object
1789  * throws.
1790  *
1791  * Since 2.0.4
1792  */
1793 template <class Ret>
1794 Cgu::IntrusivePtr<Cgu::Thread::Future<Ret>> make_future(const std::function<Ret(void)>& func) {
1795  return Cgu::Thread::Future<Ret>::make(func);
1796 }
1797 
1798 /**
1799  * A convenience helper function which calls
1800  * Cgu::Thread::Future::make() to obtain a Future object without the
1801  * need to specify the return value of the function represented by the
1802  * new object: that is deduced from the signature of that function.
1803  * This is useful shorthand when also employed with the C++11 'auto'
1804  * keyword.
1805  * @exception std::bad_alloc It might throw std::bad_alloc if memory
1806  * is exhausted and the system throws in that case. (This exception
1807  * will not be thrown if the library has been installed using the
1808  * --with-glib-memory-slices-no-compat configuration option: instead
1809  * glib will terminate the program if it is unable to obtain memory
1810  * from the operating system.)
1811  * @exception Cgu::Thread::MutexError It might throw
1812  * Cgu::Thread::MutexError if initialisation of the contained mutex
1813  * fails. (It is often not worth checking for this, as it means
1814  * either memory is exhausted or pthread has run out of other
1815  * resources to create new mutexes.)
1816  * @exception Cgu::Thread::CondError It might throw
1817  * Cgu::Thread::CondError if initialisation of the contained condition
1818  * variable fails. (It is often not worth checking for this, as it
1819  * means either memory is exhausted or pthread has run out of other
1820  * resources to create new condition variables.)
1821  * @note This method will also throw if the copy or move constructor
1822  * of a bound argument throws, or the default constructor of the
1823  * return value type of the function represented by the new object
1824  * throws.
1825  *
1826  * Since 2.0.4
1827  */
1828 template <class Ret>
1829 Cgu::IntrusivePtr<Cgu::Thread::Future<Ret>> make_future(std::function<Ret(void)>&& func) {
1830  return Cgu::Thread::Future<Ret>::make(std::move(func));
1831 }
1832 
1833 } // namespace Thread
1834 
1835 } // namespace Cgu
1836 
1837 #include <c++-gtk-utils/future.tpp>
1838 
1839 #endif