c++-gtk-utils
thread.h
Go to the documentation of this file.
1 /* Copyright (C) 2005 to 2015 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 c++-gtk-utils
20  sub-directory); if not, write to the Free Software Foundation, Inc.,
21  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 
23 */
24 
25 #ifndef CGU_THREAD_H
26 #define CGU_THREAD_H
27 
28 #include <memory> // for std::unique_ptr or std::auto_ptr
29 #include <utility> // for std::move
30 
31 #include <pthread.h>
32 
33 #include <c++-gtk-utils/callback.h>
34 #include <c++-gtk-utils/mutex.h>
36 
37 namespace Cgu {
38 
39 namespace Thread {
40 
41 /**
42  * @class Cgu::Thread::Thread thread.h c++-gtk-utils/thread.h
43  * @brief A class representing a pthread thread.
44  * @sa Thread::Mutex Thread::Mutex::Lock Thread::Cond Thread::Future Thread::JoinableHandle
45  *
46  * The Thread class encapsulates a pthread thread. It can start, join
47  * and cancel a thread.
48  *
49  * The Thread class, and the other thread related classes provided by
50  * this library such as Mutex, RecMutex, RWLock, CancelBlock and Cond,
51  * can be used interchangeably with (and mixed with) GThread objects
52  * and functions, and with GMutex, GRecMutex, GRWLock, GCond and
53  * similar, as they all use pthreads underneath on POSIX and other
54  * unix-like OSes.
55  *
56  * In addition, the thread related classes provided by this library
57  * can be used in conjunction with threads started with C++11/14
58  * threading facilities, and vice versa, as in C++11/14 on unix-like
59  * OSes these facilities are likely to be built on top of pthreads
60  * (for which purpose C++11/14 provides the std::native_handle_type
61  * type and std::thread::native_handle() function). Even where they
62  * are not, they will use the same threading primitives provided by
63  * the kernel: ยง30.3 of the C++11 standard states, albeit
64  * non-normatively, that "These threads [std::thread threads] are
65  * intended to map one-to-one with operating system threads".
66  *
67  * If in doubt, always use this library's thread related classes as
68  * they are guaranteed to be compatible with glib/gtk+ and with the
69  * native platform libraries. However, such doubts are in practice
70  * unnecessary: it is in practice inconceivable that C++11/14's
71  * threading implementation will be incompatible with the platform's
72  * native threading implementation (pthreads), because any practical
73  * program using C++11/14 threads is going to call into platform
74  * libraries, and those libraries may themselves be threaded or make
75  * thread related calls. So use whichever appears to suit the
76  * particular case better.
77  *
78  * @anchor ThreadsAnchor
79  * @b c++-gtk-utils @b library @b and @b C++11/14 @b threads
80  *
81  * As mentioned above, the thread facilities provided by this library
82  * can be freely interchanged with the threading facilities provided
83  * by C++11/14.
84  *
85  * The main features available from this library and not C++11/14 are
86  * thread cancellation and the associated Cgu::Thread::CancelBlock
87  * class, the Cgu::Thread::JoinableHandle class for scoped joinable
88  * thread handling and the Cgu::Thread::TaskManager class for running
89  * and composing tasks on a thread pool.
90  *
91  * C++11/14 does not provide thread cancellation or interruption
92  * support, and C++ will never be able to do so on a complete basis
93  * because to do so requires support from the underlying OS, which
94  * therefore makes it platform specific (in this case, POSIX
95  * specific): cancellation is only of limited use if it cannot
96  * reliably interrupt blocking system calls. The POSIX specification
97  * sets out the interruptible cancellation points in System
98  * Interfaces, section 2.9.5, Cancellation Points, and in effect
99  * specifies all the system calls which can block as cancellation
100  * points.
101  *
102  * Whether, in C++ programs, destructors of local objects in the
103  * cancelled thread are called is also system specific and is not
104  * specified by POSIX. Most modern commercial unixes, and recent
105  * linux/BSD distributions based on NPTL (in the case of Linux, those
106  * based on 2.6/3.x/4.x kernels), will unwind the stack and call
107  * destructors on thread cancellation by means of a pseudo-exception,
108  * but older distributions relying on the former linuxthreads
109  * implementation will not. Therefore for maximum portability
110  * cancellation would only be used where there are plain data
111  * structures/built-in types in existence in local scope when it
112  * occurs, and if there is anything in free store to be released
113  * clean-ups would be implemented with
114  * pthread_cleanup_push()/pthread_cleanup_pop(). This should be
115  * controlled with pthread_setcancelstate() and/or the CancelBlock
116  * class to choose the cancellation point.
117  *
118  * One of the (perhaps odd) features of C++11/14 threads is that if
119  * the destructor of a std::thread object is called which represents a
120  * joinable thread which has not been detach()ed or join()ed, the
121  * whole program is terminated with a call to std::terminate(), which
122  * makes it difficult to use in the presence of exceptions. Often
123  * what is wanted however is for join() to be called on a joinable
124  * thread where the associated thread object goes out of scope, or
125  * (provided it is done carefully and knowingly) for detach() to be
126  * called. The Cgu::Thread::JoinableHandle class can be used where
127  * either of these two is the appropriate response to this situation.
128  *
129  * In addition, the c++-gtk-utils library provides the following which
130  * are not present in C++11 and/or C++14: a guaranteed monotonic clock
131  * on timed condition variable waits where the operating system
132  * supports them; read-write locks/shared mutexes (present in C++14
133  * but not C++11); a Cgu::Thread::Future object which is more
134  * intuitive to use than C++11/14 futures and features a built in
135  * Cgu::SafeEmitter object which emits when the particular task has
136  * completed, and (since version 2.0.2) has associated
137  * Cgu::Thread::Future::when() methods for passing the result to a
138  * glib main loop; and (since version 2.0.19)
139  * Cgu::Thread::parallel_for_each() and
140  * Cgu::Thread::parallel_transform() functions for use with
141  * Cgu::Thread::TaskManager objects.
142  *
143  * @b c++-gtk-utils @b library @b and @b gthreads
144  *
145  * As mentioned above, the thread facilities provided by this library
146  * can be freely interchanged with the threading facilities provided
147  * by glib.
148  *
149  * The main features available with this thread implementation and not
150  * GThreads are thread cancellation, the mutex scoped locking classes
151  * Cgu::Thread::Mutex::Lock and Cgu::Thread::RecMutex::Lock, the
152  * joinable thread scoped management class Cgu::Thread::JoinableHandle
153  * and the Cgu::Thread::Future class (abstracting thread functions
154  * which provide a result).
155  *
156  * There is no need from the perspective of this class to call
157  * g_thread_init() before Cgu::Thread::Thread::start() is called, but
158  * prior to glib version 2.32 glib itself is not thread-safe without
159  * g_thread_init(), so where this class is used with glib < 2.32,
160  * g_thread_init() should be called at program initialization.
161  *
162  * See @ref Threading for particulars about GTK+ thread safety.
163  */
164 
165 
166 class Thread {
167  pthread_t thread;
168  // private constructor - this class can only be created with Thread::start
169  Thread() {}
170 public:
171 /**
172  * This class cannot be copied: it is intended to be held by
173  * std::unique_ptr. The copy constructor is deleted.
174  */
175  Thread(const Thread&) = delete;
176 
177 /**
178  * This class cannot be copied: it is intended to be held by
179  * std::unique_ptr. The assignment operator is deleted.
180  */
181  Thread& operator=(const Thread&) = delete;
182 
183 /**
184  * Cancels the thread represented by this Thread object. It can be
185  * called by any thread. The effect is undefined if the thread
186  * represented by this Thread object has both (a) already terminated
187  * and (b) been detached or had a call to join() made for it.
188  * Accordingly, if the user is not able to establish from the program
189  * logic whether the thread has terminated, the thread must be created
190  * as joinable and cancel() must not be called after a call to
191  * detach() has been made or a call to join() has returned. A
192  * Thread::JoinableHandle object can used to ensure this. It does not
193  * throw.
194  * @note 1. Use this method with care - sometimes its use is
195  * unavoidable but destructors for local objects may not be called if
196  * a thread exits by virtue of a call to cancel() (that depends on the
197  * implementation). Most modern commercial unixes, and recent
198  * linux/BSD distributions based on NPTL, will unwind the stack and
199  * call destructors on thread cancellation by means of a
200  * pseudo-exception, but older distributions relying on the former
201  * linuxthreads implementation will not. Therefore for maximum
202  * portability only have plain data structures/built-in types in
203  * existence in local scope when it occurs and if there is anything in
204  * free store to be released implement clean-ups with
205  * pthread_cleanup_push()/pthread_cleanup_pop(). This should be
206  * controlled with pthread_setcancelstate() and/or the CancelBlock
207  * class to choose the cancellation point.
208  * @note 2. When using thread cancellation, do not allow a
209  * cancellation pseudo-exception to propagate through a function with
210  * a 'noexcept' or 'noexcept(true)' specifier, as the cancellation
211  * pseudo-exception would be incompatible with such a specifier and
212  * std::terminate may be called.
213  * @sa Cgu::Thread::Exit
214  */
215  void cancel() {pthread_cancel(thread);}
216 
217 /**
218  * Joins the thread represented by this Thread object (that is, waits
219  * for it to terminate). It can only be called by one thread, which
220  * can be any thread other than the one represented by this Thread
221  * object. The result is undefined if the thread represented by this
222  * Thread object is or was detached or join() has already been called
223  * for it (a Thread::JoinableHandle object will however give a defined
224  * result in such cases for threads originally started as joinable).
225  * It does not throw.
226  */
227  void join() {pthread_join(thread, 0);}
228 
229 /**
230  * Detaches the thread represented by this Thread object where it is
231  * joinable, so as to make it unjoinable. The effect is undefined if
232  * the thread is already unjoinable (a Thread::JoinableHandle object
233  * will however give a defined result in such cases for threads
234  * originally started as joinable). It does not throw.
235  */
236  void detach() {pthread_detach(thread);}
237 
238 /**
239  * Specifies whether the calling thread is the same thread as is
240  * represented by this Thread object. The effect is undefined if the
241  * thread represented by this Thread object has both (a) already
242  * terminated and (b) been detached or had a call to join() made for
243  * it. Accordingly, if the user is not able to establish from the
244  * program logic whether the thread has terminated, the thread must be
245  * created as joinable and is_caller() must not be called after a call
246  * to detach() has been made or a call to join() has returned. A
247  * Thread::JoinableHandle object can used to ensure this. This method
248  * does not throw.
249  * @return Returns true if the caller is in the thread represented by
250  * this Thread object.
251  */
252  bool is_caller() {return pthread_equal(thread, pthread_self());}
253 
254 /**
255  * Starts a new thread. It can be called by any thread.
256  * @param cb A callback object (created by Callback::make(),
257  * Callback::make_ref() or Callback::lambda()) encapsulating the
258  * function to be executed by the new thread. The Thread object
259  * returned by this function will take ownership of the callback: it
260  * will automatically be deleted either by the new thread when it has
261  * finished with it, or by this method in the calling thread if the
262  * attempt to start a new thread fails (including if std::bad_alloc is
263  * thrown).
264  * @param joinable Whether the join() method may be called in relation
265  * to the new thread.
266  * @return A Thread object representing the new thread which has been
267  * started, held by a std::unique_ptr object as it has single
268  * ownership semantics. The std::unique_ptr object will be empty
269  * (that is std::unique_ptr<Cgu::Thread::Thread>::get() will return 0)
270  * if the thread did not start correctly, which would mean that memory
271  * is exhausted, the pthread thread limit has been reached or pthread
272  * has run out of other resources to start new threads.
273  * @exception std::bad_alloc This method might throw std::bad_alloc if
274  * memory is exhausted and the system throws in that case. (This
275  * exception will not be thrown if the library has been installed
276  * using the \--with-glib-memory-slices-no-compat configuration
277  * option: instead glib will terminate the program if it is unable to
278  * obtain memory from the operating system.) If this exception is
279  * thrown, the thread will not have started.
280  * @note 1. The thread will keep running even if the return value of
281  * start() goes out of scope (but it will no longer be possible to
282  * call any of the methods in this class for it, which is fine if the
283  * thread is not started as joinable and it is not intended to cancel
284  * it).
285  * @note 2. If the thread is started with the joinable attribute, the
286  * user must subsequently either call the join() or the detach()
287  * method, as otherwise a resource leak may occur (the destructor of
288  * this class does not call detach() automatically). Alternatively,
289  * the return value of this method can be passed to a
290  * Thread::JoinableHandle object which will do this automatically in
291  * the Thread::JoinableHandle object's destructor.
292  * @note 3. Any Thread::Exit exception thrown from the function
293  * executed by the new thread will be caught and consumed. The thread
294  * will safely terminate and unwind the stack in so doing.
295  * @note 4. If any uncaught exception other than Thread::Exit is
296  * allowed to propagate from the initial function executed by the new
297  * thread, the exception is not consumed (NPTL's forced stack
298  * unwinding on cancellation does not permit catching with an ellipsis
299  * argument without rethrowing, and even if it did permit it, the
300  * result would be an unreported error). The C++11 standard requires
301  * std::terminate() to be called in such a case and so the entire
302  * program terminated. Accordingly, a user must make sure that no
303  * exceptions, other than Thread::Exit or any cancellation
304  * pseudo-exception, can propagate from the initial function executed
305  * by the new thread. This includes ensuring that, for any argument
306  * passed to that function which is not a built-in type and which is
307  * not taken by the function by const or non-const reference, the
308  * argument type's copy constructor does not throw.
309  * @note 5. If the library is compiled using the \--with-auto-ptr
310  * configuration option, then this function will return a
311  * Thread::Thread object by std::auto_ptr instead of std::unique_ptr
312  * in order to retain compatibility with the 1.2 series of the
313  * library.
314  */
315 #ifdef CGU_USE_AUTO_PTR
316  static std::auto_ptr<Cgu::Thread::Thread> start(const Cgu::Callback::Callback* cb,
317  bool joinable);
318 #else
319  static std::unique_ptr<Cgu::Thread::Thread> start(const Cgu::Callback::Callback* cb,
320  bool joinable);
321 #endif
322 
323 #ifdef CGU_USE_GLIB_MEMORY_SLICES_NO_COMPAT
325 #endif
326 };
327 
328 /**
329  * @class Cgu::Thread::JoinableHandle thread.h c++-gtk-utils/thread.h
330  * @brief A class wrapping a Thread::Thread object representing a
331  * joinable thread.
332  * @sa Thread::Thread Thread::Future
333  *
334  * This class enables a joinable thread to be made more easily
335  * exception safe. It can also be used to provide that a joinable
336  * thread is not detached or joined while other methods dependent on
337  * that might still be called, and to provide a defined result where
338  * there are multiple calls to join() and/or detach(). When it is
339  * destroyed, it will either detach or join the thread represented by
340  * the wrapped Thread::Thread object unless it has previously been
341  * detached or joined using the detach() or join() methods, so
342  * avoiding thread resource leaks. Whether it will detach() or join()
343  * on destruction depends on the Thread::JoinableHandle::Action
344  * argument passed to the
345  * Thread::JoinableHandle::JoinableHandle(std::unique_ptr<Thread::Thread>,
346  * Action) constructor.
347  *
348  * Passing Thread::JoinableHandle::detach_on_exit to that argument is
349  * not always the correct choice where the thread callback has been
350  * bound to a reference argument in local scope and an exception might
351  * be thrown, because the thread will keep running after the
352  * Thread::JoinableHandle object and other local variables have
353  * (because of the exception) gone out of scope. Consider the
354  * following trivial parallelized calculation example:
355  *
356  * @code
357  * std::vector<int> get_readings();
358  * void get_mean(const std::vector<int>& v, int& result);
359  * void get_std_deviation(const std::vector<int>& v, int& result); // might throw
360  * void show_result(int mean, int deviation);
361  *
362  * using namespace Cgu;
363  * void do_calc() {
364  * int i, j;
365  * std::vector<int> v = get_readings();
366  * std::unique_ptr<Thread::Thread> t =
367  * Thread::Thread::start(Callback::lambda<>(std::bind(&get_mean, std::cref(v), std::ref(i))), true);
368  * if (t.get()) { // checks whether thread started correctly
369  * get_std_deviation(v, j);
370  * t->join();
371  * show_result(i, j);
372  * }
373  * }
374  * @endcode
375  *
376  * If get_std_deviation() throws, as well as there being a potential
377  * thread resource leak by virtue of no join being made, the thread
378  * executing get_mean() will continue running and attempt to access
379  * variable v, and put its result in variable i, which may by then
380  * both be out of scope. To deal with such a case, the thread could
381  * be wrapped in a Thread::JoinableHandle object which joins on exit
382  * rather than detaches, for example:
383  *
384  * @code
385  * ...
386  * using namespace Cgu;
387  * void do_calc() {
388  * int i, j;
389  * std::vector<int> v = get_readings();
390  * Thread::JoinableHandle t{Thread::Thread::start(Callback::lambda<>(std::bind(&get_mean, std::cref(v), std::ref(i))), true),
391  * Thread::JoinableHandle::join_on_exit};
392  * if (t.is_managing()) { // checks whether thread started correctly
393  * get_std_deviation(v, j);
394  * t.join();
395  * show_result(i, j);
396  * }
397  * }
398  * @endcode
399  *
400  * Better still, however, would be to use Cgu::Thread::Future in this
401  * kind of usage, namely a usage where a worker thread is intended to
402  * provide a result for inspection.
403  *
404  * @note These examples assume that the std::vector library
405  * implementation permits concurrent reads of a vector object by
406  * different threads. Whether that is the case depends on the
407  * documentation of the library concerned (if designed for a
408  * multi-threaded environment, most will permit this, and it is
409  * required for a fully conforming C++11 library implementation).
410  */
412 public:
414 
415 private:
416  Mutex mutex; // make this the first member so the constructors are strongly exception safe
417  Action action;
418  bool detached;
419  std::unique_ptr<Cgu::Thread::Thread> thread;
420 
421 public:
422 /**
423  * Cancels the thread represented by the wrapped Thread::Thread
424  * object. It can be called by any thread. The effect is undefined
425  * if when called the thread represented by the wrapped Thread::Thread
426  * object has both (a) already terminated and (b) had a call to join()
427  * or detach() made for it. Accordingly, if the user is not able to
428  * establish from the program logic whether the thread has terminated,
429  * cancel() must not be called after a call to detach() has been made
430  * or a call to join() has returned: this can be ensured by only
431  * detaching or joining via this object's destructor (that is, by not
432  * using the explicit detach() and join() methods). This method does
433  * not throw.
434  * @note Use this method with care - see Thread::cancel() for further
435  * information.
436  */
437  void cancel();
438 
439 /**
440  * Joins the thread represented by the wrapped Thread::Thread object
441  * (that is, waits for it to terminate), unless the detach() or join()
442  * method has previously been called in which case this call does
443  * nothing. It can be called by any thread other than the one
444  * represented by the wrapped Thread::Thread object, but only one
445  * thread can wait on it: if one thread (thread A) calls it while
446  * another thread (thread B) is already blocking on it, thread A's
447  * call to this method will return immediately and return false. It
448  * does not throw.
449  * @return true if a successful join() has been accomplished (that is,
450  * detach() or join() have not previously been called), otherwise
451  * false.
452  */
453  bool join();
454 
455 /**
456  * Detaches the thread represented by this Thread::Thread object, so
457  * as to make it unjoinable, unless the detach() or join() method has
458  * previously been called in which case this call does nothing. It
459  * does not throw.
460  */
461  void detach();
462 
463 /**
464  * Specifies whether the calling thread is the same thread as is
465  * represented by the wrapped Thread::Thread object. It can be called
466  * by any thread. The effect is undefined if the thread represented
467  * by the wrapped Thread::Thread object has both (a) already
468  * terminated and (b) had a call to join() or detach() made for it.
469  * Accordingly, if the user is not able to establish from the program
470  * logic whether the thread has terminated, is_caller() must not be
471  * called after a call to detach() has been made or a call to join()
472  * has returned: this can be ensured by only detaching or joining via
473  * this object's destructor (that is, by not using the explicit
474  * detach() and join() methods). This method does not throw.
475  * @return Returns true if the caller is in the thread represented by
476  * the wrapped Thread::Thread object. If not, or this JoinableHandle
477  * does not wrap any Thread object, then returns false.
478  */
479  bool is_caller();
480 
481 /**
482  * Specifies whether this JoinableHandle object has been initialized
483  * with a Thread::Thread object representing a correctly started
484  * thread in respect of which neither JoinableHandle::detach() nor
485  * JoinableHandle::join() has been called. It can be called by any
486  * thread. It is principally intended to enable the constructor
487  * taking a std::unique_ptr<Cgu::Thread::Thread> object to be directly
488  * initialized by a call to Thread::Thread::start(), by providing a
489  * means for the thread calling Thread::Thread::start() to check
490  * afterwards that the new thread did, in fact, start correctly. Note
491  * that this method will return true even after the thread has
492  * finished, provided neither the join() nor detach() method has been
493  * called.
494  * @return Returns true if this object has been initialized by a
495  * Thread::Thread object representing a correctly started thread in
496  * respect of which neither JoinableHandle::detach() nor
497  * JoinableHandle::join() has been called, otherwise false.
498  */
499  bool is_managing();
500 
501 /**
502  * Moves one JoinableHandle object to another JoinableHandle object.
503  * This is a move operation which transfers ownership to the assignee,
504  * as the handles store their Thread::Thread object by
505  * std::unique_ptr<>. Any existing thread managed by the assignee
506  * prior to the move will be detached if it has not already been
507  * detached or joined. This method will not throw.
508  * @param h The assignor/movant, which will cease to hold a valid
509  * Thread::Thread object after the move has taken place.
510  * @return A reference to the assignee JoinableHandle object after
511  * assignment.
512  * @note 1. This method is thread safe as regards the assignee (the
513  * object assigned to), but no synchronization is carried out with
514  * respect to the rvalue assignor/movant. This is because temporaries
515  * are only visible and accessible in the thread carrying out the move
516  * operation and synchronization for them would represent pointless
517  * overhead. In a case where the user uses std::move to force a move
518  * from a named object, and that named object's lifetime is managed by
519  * (or the object is otherwise accessed by) a different thread than
520  * the one making the move, the user must carry out her own
521  * synchronization with respect to that different thread, as the named
522  * object will be mutated by the move.
523  * @note 2. If the library is compiled using the \--with-auto-ptr
524  * configuration option, then this operator's signature is
525  * JoinableHandle& operator=(JoinableHandle& h) in order to retain
526  * compatibility with the 1.2 series of the library.
527  */
528 #ifdef CGU_USE_AUTO_PTR
530 #else
532 #endif
533 
534 /**
535  * This constructor initializes a new JoinableHandle object with a
536  * std::unique_ptr<Thread::Thread> object, as provided by
537  * Thread::Thread::start(). This is a move operation which transfers
538  * ownership to the new object.
539  * @param thr The initializing Thread::Thread object (which must have
540  * been created as joinable) passed by a std::unique_ptr smart
541  * pointer. This is a move operation.
542  * @param act Either Thread::JoinableHandle::detach_on_exit (which
543  * will cause the destructor to detach the thread if it has not
544  * previously been detached or joined) or
545  * Thread::JoinableHandle::join_on_exit (which will cause the
546  * destructor to join the thread if it has not previously been
547  * detached or joined).
548  * @exception Cgu::Thread::MutexError Throws this exception if
549  * initialization of the internal mutex fails. The constructor is
550  * strongly exception safe: if Cgu::Thread::MutexError is thrown, the
551  * initializing std::unique_ptr<Cgu::Thread::Thread> object will be
552  * left unchanged. (It is often not worth checking for this
553  * exception, as it means either memory is exhausted or pthread has
554  * run out of other resources to create new mutexes.)
555  * @note 1. It is not necessary to check that the thread parameter
556  * represents a correctly started thread (that is, that thr.get() does
557  * not return 0) before this constructor is invoked, because that can
558  * be done after construction by calling JoinableHandle::is_managing()
559  * (a JoinableHangle object can safely handle a case where thr.get()
560  * does return 0). This enables a JoinableHandle object to be
561  * directly initialized by this constructor from a call to
562  * Thread::Thread::start().
563  * @note 2. No synchronization is carried out with respect to the
564  * initializing std::unique_ptr object. This is because such an
565  * object is usually passed to this constructor as a temporary, which
566  * is only visible and accessible in the thread carrying out the move
567  * operation, in which case synchronization would represent pointless
568  * overhead. In a case where the user uses std::move to force a move
569  * from a named std::unique_ptr object, and that named object's
570  * lifetime is managed by (or the object is otherwise accessed by) a
571  * different thread than the one making the move, the user must carry
572  * out her own synchronization with respect to that different thread,
573  * as the initializing std::unique_ptr object will be mutated by the
574  * move.
575  * @note 3. If the library is compiled using the \--with-auto-ptr
576  * configuration option, then this constructor's signature is
577  * JoinableHandle(std::auto_ptr<Cgu::Thread::Thread> thr, Action act)
578  * in order to retain compatibility with the 1.2 series of the library
579  * @sa JoinableHandle::is_managing().
580  */
581 #ifdef CGU_USE_AUTO_PTR
582  JoinableHandle(std::auto_ptr<Cgu::Thread::Thread> thr, Action act): action(act), detached(false), thread(thr.release()) {}
583 #else
584  JoinableHandle(std::unique_ptr<Cgu::Thread::Thread> thr, Action act): action(act), detached(false), thread(std::move(thr)) {}
585 #endif
586 
587 /**
588  * This constructor initializes a new JoinableHandle object with an
589  * existing JoinableHandle object. This is a move operation which
590  * transfers ownership to the new object.
591  * @param h The initializing JoinableHandle object, which will cease
592  * to hold a valid Thread::Thread object after the initialization has
593  * taken place.
594  * @exception Cgu::Thread::MutexError Throws this exception if
595  * initialization of the internal mutex fails. The constructor is
596  * strongly exception safe: if Cgu::Thread::MutexError is thrown, the
597  * initializing Cgu::Thread::JoinableHandle object will be left
598  * unchanged. (It is often not worth checking for this exception, as
599  * it means either memory is exhausted or pthread has run out of other
600  * resources to create new mutexes.)
601  * @note 1. No synchronization is carried out with respect to the
602  * initializing rvalue. This is because temporaries are only visible
603  * and accessible in the thread carrying out the move operation and
604  * synchronization for them would represent pointless overhead. In a
605  * case where a user uses std::move to force a move from a named
606  * object, and that named object's lifetime is managed by (or the
607  * object is otherwise accessed by) a different thread than the one
608  * making the move, the user must carry out her own synchronization
609  * with respect to that different thread, as the named object will be
610  * mutated by the move.
611  * @note 2. If the library is compiled using the \--with-auto-ptr
612  * configuration option, then this constructor's signature is
613  * JoinableHandle(JoinableHandle& h) in order to retain compatibility
614  * with the 1.2 series of the library.
615  */
616 #ifdef CGU_USE_AUTO_PTR
617  JoinableHandle(JoinableHandle& h): action(h.action), detached(h.detached), thread(std::move(h.thread)) {}
618 #else
619  JoinableHandle(JoinableHandle&& h): action(h.action), detached(h.detached), thread(std::move(h.thread)) {}
620 #endif
621 
622 /**
623  * The default constructor. Nothing is managed until the move
624  * assignment operator has been called.
625  * @exception Cgu::Thread::MutexError Throws this exception if
626  * initialization of the internal mutex fails. (It is often not worth
627  * checking for this exception, as it means either memory is exhausted
628  * or pthread has run out of other resources to create new mutexes.)
629  *
630  * Since 2.0.8
631  */
632  JoinableHandle(): action(detach_on_exit), detached(true) {}
633 
634 /**
635  * The destructor will detach a managed thread (if the
636  * Thread::JoinableHandle::detach_on_exit flag is set) or join it (if
637  * the Thread::JoinableHandle::join_on_exit flag is set), unless it
638  * has previously been detached or joined with the detach() or join()
639  * methods. The destructor is thread safe (any thread may destroy the
640  * JoinableHandle object). The destructor will not throw.
641  */
642  ~JoinableHandle();
643 
644 /* Only has effect if --with-glib-memory-slices-compat or
645  * --with-glib-memory-slices-no-compat option picked */
647 };
648 
649 /**
650  * @class CancelBlock thread.h c++-gtk-utils/thread.h
651  * @brief A class enabling the cancellation state of a thread to be
652  * controlled.
653  *
654  * A class enabling the cancellation state of a thread to be
655  * controlled, so as to provide exception safe cancellation state
656  * changes. When a CancelBlock object goes out of scope, the thread's
657  * cancellation state is returned to the state it was in immediately
658  * prior to the object's construction.
659  *
660  * Cancellation state can be changed before a CancelBlock object goes
661  * out of scope by calling its block() and unblock() methods.
662  * However, care should be taken if calling unblock() for the purpose
663  * of enabling thread cancellation while the CancelBlock object is
664  * still in existence: this should normally only be done if the
665  * thread's cancellation state at the time the CancelBlock object was
666  * constructed (which is the cancellation state to which the thread
667  * will be restored when the object goes out of scope) was
668  * PTHREAD_CANCEL_DISABLE. This is because when a thread begins
669  * cancellation the POSIX standard states that it will automatically
670  * switch itself into a PTHREAD_CANCEL_DISABLE state (see System
671  * Interfaces, section 2.9.5, Thread Cancellation Cleanup Handlers),
672  * and the POSIX standard further states that the behaviour is
673  * undefined if a cancellation handler attempts to enable cancellation
674  * again while the thread is cleaning up - and any thread
675  * implementation such as NPTL which unwinds the stack on cancellation
676  * will do so if the CancelBlock's destructor would restore to
677  * PTHREAD_CANCEL_ENABLE state. Whilst it is to be expected that any
678  * cancellation stack unwinding implementation will behave sensibly in
679  * these circumstances, this is not mandated by POSIX, so making code
680  * relying on this less portable.
681  *
682  * For these reasons, the same care should be exercised if passing
683  * 'false' to the CancelBlock constructor's 'blocking' argument.
684  */
685 
686 class CancelBlock {
687  int starting_state;
688 public:
689 /**
690  * This class cannot be copied. The copy constructor is deleted.
691  */
692  CancelBlock(const CancelBlock&) = delete;
693 
694 /**
695  * This class cannot be copied. The assignment operator is deleted.
696  */
697  CancelBlock& operator=(const CancelBlock&) = delete;
698 
699 /**
700  * Makes the thread uncancellable, even if the code passes through a
701  * cancellation point, while the CancelBlock object exists (when the
702  * CancelBlock object ceases to exist, cancellation state is returned
703  * to the state prior to it being constructed). It should only be
704  * called by the thread which created the CancelBlock object. This
705  * method will not throw.
706  * @param old_state Indicates the cancellation state of the calling
707  * thread immediately before this call to block() was made, either
708  * PTHREAD_CANCEL_ENABLE (if the thread was previously cancellable) or
709  * PTHREAD_CANCEL_DISABLE (if this call did nothing because the thread
710  * was already uncancellable).
711  * @return 0 if successful, else a value other than 0.
712  */
713  static int block(int& old_state) {return pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &old_state);}
714 
715 /**
716  * Makes the thread uncancellable, even if the code passes through a
717  * cancellation point, while the CancelBlock object exists (when the
718  * CancelBlock object ceases to exist, cancellation state is returned
719  * to the state prior to it being constructed). It should only be
720  * called by the thread which created the CancelBlock object. This
721  * method will not throw.
722  * @return 0 if successful, else a value other than 0.
723  */
724  static int block() {int old_state; return block(old_state);}
725 
726 /**
727  * Makes the thread cancellable while the CancelBlock object exists
728  * (when the CancelBlock object ceases to exist, cancellation state is
729  * returned to the state prior to it being constructed). It should
730  * only be called by the thread which created the CancelBlock object.
731  * This method will not throw. The 'Detailed Description' section
732  * above has information about the issues to be taken into account if
733  * a call to this method is to be made.
734  * @param old_state Indicates the cancellation state of the calling
735  * thread immediately before this call to unblock() was made, either
736  * PTHREAD_CANCEL_DISABLE (if the thread was previously uncancellable)
737  * or PTHREAD_CANCEL_ENABLE (if this call did nothing because the
738  * thread was already cancellable).
739  * @return 0 if successful, else a value other than 0.
740  */
741  static int unblock(int& old_state) {return pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &old_state);}
742 
743 /**
744  * Makes the thread cancellable while the CancelBlock object exists
745  * (when the CancelBlock object ceases to exist, cancellation state is
746  * returned to the state prior to it being constructed). It should
747  * only be called by the thread which created the CancelBlock object.
748  * This method will not throw. The 'Detailed Description' section
749  * above has information about the issues to be taken into account if
750  * a call to this method is to be made.
751  * @return 0 if successful, else a value other than 0.
752  */
753  static int unblock() {int old_state; return unblock(old_state);}
754 
755 /**
756  * Restores cancellation state to the state it was in immediately
757  * before this CancelBlock object was constructed. It should only be
758  * called by the thread which created the CancelBlock object. This
759  * method will not throw.
760  * @param old_state Indicates the cancellation state of the calling
761  * thread immediately before this call to restore() was made, either
762  * PTHREAD_CANCEL_DISABLE (if the thread was previously uncancellable)
763  * or PTHREAD_CANCEL_ENABLE (if this thread was previously
764  * cancellable).
765  * @return 0 if successful, else a value other than 0.
766  */
767  int restore(int& old_state) {return pthread_setcancelstate(starting_state, &old_state);}
768 
769 /**
770  * Restores cancellation state to the state it was in immediately
771  * before this CancelBlock object was constructed. It should only be
772  * called by the thread which created the CancelBlock object. This
773  * method will not throw.
774  * @return 0 if successful, else a value other than 0.
775  */
776  int restore() {int old_state; return restore(old_state);}
777 
778 /**
779  * The constructor will not throw.
780  * @param blocking Whether the CancelBlock object should start in
781  * blocking mode. The 'Detailed Description' section above has
782  * information about the issues to be taken into account if 'false' is
783  * passed to this parameter.
784  */
785  CancelBlock(bool blocking = true);
786 
787 /**
788  * The destructor will put the thread in the cancellation state that
789  * it was in immediately before the CancelBlock object was constructed
790  * (which might be blocking). It will not throw.
791  */
793 
794 /* Only has effect if --with-glib-memory-slices-compat or
795  * --with-glib-memory-slices-no-compat option picked */
797 };
798 
799 /**
800  * @class Exit thread.h c++-gtk-utils/thread.h
801  * @brief A class which can be thrown to terminate the throwing
802  * thread.
803  *
804  * This class can be thrown (instead of calling pthread_exit()) when a
805  * thread wishes to terminate itself and also ensure stack unwinding,
806  * so that destructors of local objects are called. It is caught
807  * automatically by the implementation of Cgu::Thread::Thread::start()
808  * so that it will only terminate the thread throwing it and not the
809  * whole process. See the Cgu::Thread::Thread::cancel() method above,
810  * for use when a thread wishes to terminate another one, and the
811  * caveats on the use of Cgu::Thread::Thread::cancel().
812  *
813  * Do not throw a Cgu::Thread::Exit object in a program with more than
814  * one main loop in order to terminate one of the threads which has
815  * its own main loop. Instead, just cause its main loop to terminate
816  * by, say, calling g_main_loop_quit() on it.
817  */
818 class Exit {};
819 
820 } // namespace Thread
821 
822 } // namespace Cgu
823 
824 #endif
Cgu::Thread::JoinableHandle::cancel
void cancel()
Cgu::Callback::CallbackArg
The callback interface class.
Definition: callback.h:522
Cgu
Definition: application.h:44
Cgu::Thread::JoinableHandle
A class wrapping a Thread::Thread object representing a joinable thread.
Definition: thread.h:411
Cgu::Thread::CancelBlock::restore
int restore(int &old_state)
Definition: thread.h:767
Cgu::Thread::Thread::cancel
void cancel()
Definition: thread.h:215
Cgu::Thread::JoinableHandle::join_on_exit
@ join_on_exit
Definition: thread.h:413
Cgu::Thread::Thread::join
void join()
Definition: thread.h:227
Cgu::Thread::CancelBlock::operator=
CancelBlock & operator=(const CancelBlock &)=delete
Cgu::Thread::JoinableHandle::JoinableHandle
JoinableHandle()
Definition: thread.h:632
Cgu::Thread::CancelBlock::~CancelBlock
~CancelBlock()
Definition: thread.h:792
Cgu::Thread::CancelBlock::CancelBlock
CancelBlock(const CancelBlock &)=delete
Cgu::Thread::CancelBlock::restore
int restore()
Definition: thread.h:776
callback.h
This file provides classes for type erasure.
Cgu::Thread::CancelBlock::block
static int block()
Definition: thread.h:724
Cgu::Thread::JoinableHandle::Action
Action
Definition: thread.h:413
Cgu::Thread::Exit
A class which can be thrown to terminate the throwing thread.
Definition: thread.h:818
Cgu::Thread::CancelBlock::block
static int block(int &old_state)
Definition: thread.h:713
Cgu::Thread::JoinableHandle::~JoinableHandle
~JoinableHandle()
Cgu::Thread::CancelBlock::unblock
static int unblock()
Definition: thread.h:753
Cgu::Thread::JoinableHandle::is_caller
bool is_caller()
Cgu::Thread::JoinableHandle::detach
void detach()
Cgu::Thread::JoinableHandle::JoinableHandle
JoinableHandle(JoinableHandle &&h)
Definition: thread.h:619
Cgu::Thread::JoinableHandle::is_managing
bool is_managing()
Cgu::Thread::JoinableHandle::join
bool join()
Cgu::Thread::Thread::operator=
Thread & operator=(const Thread &)=delete
CGU_GLIB_MEMORY_SLICES_FUNCS
#define CGU_GLIB_MEMORY_SLICES_FUNCS
Definition: cgu_config.h:84
Cgu::Thread::JoinableHandle::detach_on_exit
@ detach_on_exit
Definition: thread.h:413
Cgu::Thread::CancelBlock
A class enabling the cancellation state of a thread to be controlled.
Definition: thread.h:686
mutex.h
Provides wrapper classes for pthread mutexes and condition variables, and scoped locking classes for ...
Cgu::Thread::JoinableHandle::operator=
JoinableHandle & operator=(JoinableHandle &&h)
Cgu::Thread::Thread::detach
void detach()
Definition: thread.h:236
Cgu::Thread::CancelBlock::unblock
static int unblock(int &old_state)
Definition: thread.h:741
Cgu::Thread::Thread::is_caller
bool is_caller()
Definition: thread.h:252
Cgu::Thread::Mutex
A wrapper class for pthread mutexes.
Definition: mutex.h:117
Cgu::Thread::JoinableHandle::JoinableHandle
JoinableHandle(std::unique_ptr< Cgu::Thread::Thread > thr, Action act)
Definition: thread.h:584
Cgu::Thread::Thread
A class representing a pthread thread.
Definition: thread.h:166
cgu_config.h
Cgu::Thread::Thread::start
static std::unique_ptr< Cgu::Thread::Thread > start(const Cgu::Callback::Callback *cb, bool joinable)