mailboxRefcount

To ensure that no other thread is concurrently enqueueing a message when this thread shuts down, maintain an atomic refcount. Enqueueing threads atomically increment the count from a nonzero number to acquire the mailbox and decrement the count when they finish. When this thread shuts down it will atomically decrement the count and wait until it reaches 0, at which point the mailbox is considered closed and no further messages will be enqueued. _Atomic int mailbox_refcount;