CX Framework
Cross-platform C utility framework
Loading...
Searching...
No Matches
SharedEvent Struct Reference

#include <event.h>

Public Member Functions

 atomic (uintptr) ref
 Reference count.
 

Data Fields

Event ev
 Underlying event.
 

Detailed Description

Reference-counted event for shared ownership between threads

A common pattern is an event shared between two threads where one signals the other when something is complete. This makes managing the event's lifetime difficult, as it's not safe for it to live on the stack or for one thread to free it.

SharedEvent provides a reference-counted wrapper that can be safely cleaned up when both threads are done with it using sheventAcquire() and sheventRelease().

Example:

int workerThread(Thread *self) {
// do work...
eventSignal(&ev->ev);
sheventRelease(&ev); // worker releases its reference
return 0;
}
// Main thread creates shared event and passes to worker
SharedEvent *completion = sheventCreate(0);
Thread *worker = thrCreate(workerThread, _S"worker",
stvar(ptr, sheventAcquire(completion)));
// Wait for worker to complete
eventWait(&completion->ev);
// Clean up
sheventRelease(&completion); // main thread releases its reference
thrRelease(&worker);
#define _S
Creates a static ASCII string from a string literal.
Definition strbase.h:392
#define stvar(typen, val)
Definition stvar.h:153
#define stvlNextPtr(list)
Definition stvar.h:537
#define thrRelease(pthread)
Definition thread.h:164
SharedEvent * sheventCreate(uint32 flags)
void eventWait(Event *e)
Definition event.h:160
void sheventRelease(SharedEvent **pev)
#define eventSignal(e)
Definition event.h:125
SharedEvent * sheventAcquire(SharedEvent *ev)
Event ev
Underlying event.
Definition event.h:98

Definition at line 97 of file event.h.


The documentation for this struct was generated from the following file: