glibmm  2.62.0
About: glibmm is a set of C++ bindings for glib, including cross-platform APIs such as a std::string-like UTF8 string class, string utility methods, such as a text encoding converter API, file access, and threads.
  Fossies Dox: glibmm-2.62.0.tar.xz  ("inofficial" and yet experimental doxygen-generated source code documentation)  

Glib::Dispatcher Class Reference

Signal class for inter-thread communication. More...

#include <dispatcher.h>

Collaboration diagram for Glib::Dispatcher:
[legend]

Public Member Functions

 Dispatcher ()
 Create new Dispatcher instance using the default main context. More...
 
 Dispatcher (const Dispatcher &)=delete
 
Dispatcheroperator= (const Dispatcher &)=delete
 
 Dispatcher (const Glib::RefPtr< MainContext > &context)
 Create new Dispatcher instance using an arbitrary main context. More...
 
 ~Dispatcher () noexcept
 
void emit ()
 
void operator() ()
 
sigc::connection connect (const sigc::slot< void > &slot)
 
sigc::connection connect (sigc::slot< void > &&slot)
 

Private Attributes

sigc::signal< void > signal_
 
DispatchNotifiernotifier_
 

Detailed Description

Signal class for inter-thread communication.

Glib::Dispatcher works similar to sigc::signal<void>. But unlike normal signals, the notification happens asynchronously through a pipe. This is a simple and efficient way of communicating between threads, and especially useful in a thread model with a single GUI thread.

No mutex locking is involved, apart from the operating system's internal I/O locking. That implies some usage rules:

  • Only one thread may connect to the signal and receive notification, but multiple senders are allowed even without locking.
  • The GLib main loop must run in the receiving thread (this will be the GUI thread usually).
  • The Dispatcher object must be instantiated by the receiver thread.
  • The Dispatcher object should be instantiated before creating any of the sender threads, if you want to avoid extra locking.
  • The Dispatcher object must be deleted by the receiver thread.
  • All Dispatcher objects instantiated by the same receiver thread must use the same main context.

Notes about performance:

  • After instantiation, Glib::Dispatcher will never lock any mutexes on its own. The interaction with the GLib main loop might involve locking on the receiver side. The sender side, however, is guaranteed not to lock, except for internal locking in the write() system call.
  • All Dispatcher instances of a receiver thread share the same pipe. That is, if you use Glib::Dispatcher only to notify the GUI thread, only one pipe is created no matter how many Dispatcher objects you have.

Using Glib::Dispatcher on Windows:

Glib::Dispatcher also works on win32-based systems. Unfortunately though, the implementation cannot use a pipe on win32 and therefore does have to lock a mutex on emission, too. However, the impact on performance is likely minor and the notification still happens asynchronously. Apart from the additional lock the behavior matches the Unix implementation.

Definition at line 69 of file dispatcher.h.

Constructor & Destructor Documentation

◆ Dispatcher() [1/3]

Glib::Dispatcher::Dispatcher ( )

Create new Dispatcher instance using the default main context.

Exceptions
Glib::FileError

Definition at line 501 of file dispatcher.cc.

◆ Dispatcher() [2/3]

Glib::Dispatcher::Dispatcher ( const Dispatcher )
delete

◆ Dispatcher() [3/3]

Glib::Dispatcher::Dispatcher ( const Glib::RefPtr< MainContext > &  context)
explicit

Create new Dispatcher instance using an arbitrary main context.

Exceptions
Glib::FileError

Definition at line 506 of file dispatcher.cc.

◆ ~Dispatcher()

Glib::Dispatcher::~Dispatcher ( )
noexcept

Definition at line 511 of file dispatcher.cc.

References notifier_, and Glib::DispatchNotifier::unreference_instance().

Member Function Documentation

◆ connect() [1/2]

sigc::connection Glib::Dispatcher::connect ( const sigc::slot< void > &  slot)

Definition at line 529 of file dispatcher.cc.

References signal_.

◆ connect() [2/2]

sigc::connection Glib::Dispatcher::connect ( sigc::slot< void > &&  slot)
Since @PACKAGE_NAME@ 2.48:

Definition at line 535 of file dispatcher.cc.

References signal_.

◆ emit()

void Glib::Dispatcher::emit ( )

Definition at line 517 of file dispatcher.cc.

References notifier_, and Glib::DispatchNotifier::send_notification().

◆ operator()()

void Glib::Dispatcher::operator() ( )

Definition at line 523 of file dispatcher.cc.

References notifier_, and Glib::DispatchNotifier::send_notification().

◆ operator=()

Dispatcher& Glib::Dispatcher::operator= ( const Dispatcher )
delete

Member Data Documentation

◆ notifier_

DispatchNotifier* Glib::Dispatcher::notifier_
private

Definition at line 97 of file dispatcher.h.

Referenced by emit(), operator()(), and ~Dispatcher().

◆ signal_

sigc::signal<void> Glib::Dispatcher::signal_
private

Definition at line 96 of file dispatcher.h.

Referenced by connect().


The documentation for this class was generated from the following files: