A thread-safe multicast hap that invokes all attached handlers synchronously.
SharedHap provides the same attach/detach/emit semantics as Hap but with internal synchronization for safe use across threads. Handlers are invoked outside the internal lock to prevent deadlocks during re-entrant calls. The internal state is published with an atomic compare-and-swap on first use and every access reads it with acquire semantics, so concurrent first use from multiple threads is safe: no handler is ever lost to an initialization race.
Snapshot semantics: emit captures a snapshot of current handlers under the lock, then releases the lock before invoking handlers. Mutations during emit affect future snapshots, not the current one. A handler already captured in an in-flight snapshot may still run after detach or detachAll returns.
SharedHap is non-copyable and move-only. It uses reference-backed shared state so in-flight snapshots remain valid even if the public SharedHap value is moved or destroyed after snapshot capture.
Note
SharedHap intentionally provides no
register (detachable handle) or
emitSafe (error-reporting dispatch) counterparts. When those are needed, use Hap or NothrowHap, which are single-threaded by design.
Parameters
Args | Argument types passed to attached handlers. |
Nested Templates
SharedRecordOne attached handler in `SharedState`.
SharedStateLock-protected handler list shared by all `SharedHap` handles and threads.