# Poco

# class AsyncNotificationCenter

Library: Foundation
Package: Notifications
Header: Poco/AsyncNotificationCenter.h

## Description

AsyncNotificationCenter decouples posting of notifications from notifying subscribers by calling observers' notification handler in a dedicated thread.

It supports multiple modes of operation:

- ENQUEUE: Notifications are added to a queue, separate single thread

    asynchronously dispatches them to observers sequentially

- NOTIFY: Notifications are added to a list for each observer, multiple

    worker threads process notifications in parallel

- BOTH: Combination of both modes, notifications are enqueued and worker

    threads dispatch them to observers in parallel.

NOTIFY and BOTH mode:

These modes are only available if the compiler supports C++20 std::jthread.

Notifications can be delivered to observers in a different order than they were posted, as they are processed by multiple worker threads. Observer handlers must also be thread-safe as multiple notifications can be dispatched to the same observer in parallel.

Note about using AsyncObserver

Although it is possible to use them with AsyncNotificationCenter, it is more efficient to use NObserver.

## Inheritance

Direct Base Classes: NotificationCenter

All Base Classes: NotificationCenter

## Member Summary

Member Functions: addAsyncObserver, backlog, notifyObservers, operator =, postNotification, removeAsyncObserver, synchronousDispatch

Inherited Functions: addNObserver, addObserver, backlog, clear, countObservers, defaultCenter, hasObserver, hasObservers, mutex, notifyObservers, observersToNotify, postNotification, removeNObserver, removeObserver

## Enumerations

### AsyncMode

ENQUEUE

NOTIFY

BOTH

ENQUEUE: Notifications are enqueued in a separate thread. NOTIFY: Notifications are dispatched to observers from worker threads BOTH: Notifications are enqueued and dispatched to observers in worker threads.

## Constructors

### AsyncNotificationCenter

AsyncNotificationCenter();

Creates the AsyncNotificationCenter and starts the notifying thread.

### AsyncNotificationCenter

AsyncNotificationCenter(
const AsyncNotificationCenter & param171
) = delete;

### AsyncNotificationCenter

AsyncNotificationCenter(
AsyncNotificationCenter && param173
) = delete;

## Destructor

### ~AsyncNotificationCenter

~AsyncNotificationCenter() override;

Stops the notifying thread and destroys the AsyncNotificationCenter.

## Member Functions

### addAsyncObserver

template < class C, class N > void addAsyncObserver(
C & object,
void (C::* method)(const AutoPtr < N > &) param174,
bool (C::* matcher)(const std::string &)const param175 = nullptr
);

Convenience method for registering an AsyncObserver. Creates an AsyncObserver<C, N> with optional matcher and registers it. Usage:

    asyncNotificationCenter.addAsyncObserver(*this, &MyClass::handleNotification);
    asyncNotificationCenter.addAsyncObserver(*this, &MyClass::handleNotification, &MyClass::matchNotification);

### backlog

int backlog() const override;

Returns the number of notifications in the notification queue.

See also: Poco::NotificationCenter::backlog()

### operator =

AsyncNotificationCenter & operator = (
const AsyncNotificationCenter & param170
) = delete;

### operator =

AsyncNotificationCenter & operator = (
AsyncNotificationCenter && param172
) = delete;

### postNotification

void postNotification(
Notification::Ptr pNotification
) override;

Enqueues notification into the notification queue.

See also: Poco::NotificationCenter::postNotification()

### removeAsyncObserver

template < class C, class N > void removeAsyncObserver(
C & object,
void (C::* method)(const AutoPtr < N > &) param176,
bool (C::* matcher)(const std::string &)const param177 = nullptr
);

Convenience method for unregistering an AsyncObserver. Removes the AsyncObserver<C, N> with the given callback and matcher.

### synchronousDispatch

std::vector < NotificationResult > synchronousDispatch(
Notification::Ptr pNotification
);

Dispatches the notification synchronously to all observers that have a function for synchronous notification processing and accept the notification. This method blocks until the notification is processed by all observers. Returns results from all observers that accepted the notification.

### notifyObservers

void notifyObservers(
Notification::Ptr & pNotification
) override;

See also: Poco::NotificationCenter::notifyObservers()