2 #ifndef _GLIBMM_DISPATCHER_H
3 #define _GLIBMM_DISPATCHER_H
5 /* $Id: dispatcher.h 370 2007-01-20 10:53:28Z daniel $ */
7 /* Copyright 2002 The gtkmm Development Team
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Library General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Library General Public License for more details.
19 * You should have received a copy of the GNU Library General Public
20 * License along with this library; if not, write to the Free
21 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include <sigc++/sigc++.h>
25 #include <glibmm/main.h>
30 #ifndef DOXYGEN_SHOULD_SKIP_THIS
31 class DispatchNotifier;
34 /** Signal class for inter-thread communication.
36 * Glib::Dispatcher works similar to sigc::signal<void>. But unlike normal
37 * signals, the notification happens asynchronously through a pipe. This is
38 * a simple and efficient way of communicating between threads, and especially
39 * useful in a thread model with a single GUI thread.
41 * No mutex locking is involved, apart from the operating system's internal
42 * I/O locking. That implies some usage rules:
44 * @li Only one thread may connect to the signal and receive notification, but
45 * multiple senders are allowed even without locking.
46 * @li The GLib main loop must run in the receiving thread (this will be the
47 * GUI thread usually).
48 * @li The Dispatcher object must be instantiated by the receiver thread.
49 * @li The Dispatcher object should be instantiated before creating any of the
50 * sender threads, if you want to avoid extra locking.
52 * Notes about performance:
54 * @li After instantiation, Glib::Dispatcher will never lock any mutexes on its
55 * own. The interaction with the GLib main loop might involve locking on the
56 * @em receiver side. The @em sender side, however, is guaranteed not to lock,
57 * except for internal locking in the <tt>%write()</tt> system call.
58 * @li All Dispatcher instances of a receiver thread share the same pipe. That
59 * is, if you use Glib::Dispatcher only to notify the GUI thread, only one pipe
60 * is created no matter how many Dispatcher objects you have.
62 * Using Glib::Dispatcher on Windows:
64 * Glib::Dispatcher also works on win32-based systems. Unfortunately though,
65 * the implementation cannot use a pipe on win32 and therefore does have to
66 * lock a mutex on emission, too. However, the impact on performance is
67 * likely minor and the notification still happens asynchronously. Apart
68 * from the additional lock the behavior matches the Unix implementation.
73 /** Create new Dispatcher instance using the default main context.
74 * @throw Glib::FileError
78 /** Create new Dispatcher instance using an arbitrary main context.
79 * @throw Glib::FileError
81 explicit Dispatcher(const Glib::RefPtr<MainContext>& context);
87 sigc::connection connect(const sigc::slot<void>& slot);
90 sigc::signal<void> signal_;
91 DispatchNotifier* notifier_;
94 Dispatcher(const Dispatcher&);
95 Dispatcher& operator=(const Dispatcher&);
97 #ifndef DOXYGEN_SHOULD_SKIP_THIS
98 friend class Glib::DispatchNotifier;
102 /*! A Glib::Dispatcher example.
103 * @example thread/dispatcher.cc
108 #endif /* _GLIBMM_DISPATCHER_H */