Notifying an event file descriptor

x::eventdestroynotify is an x::ref to a reference-counted object which saves a weak reference to a event file descriptor, and which gets installed as a destructor callback to a mcguffin. When the mcguffin goes out of scope and gets destroyed, the event file descriptor gets notified.

#include <x/threads/job.H>
#include <x/eventdestroynotify.H>
#include <x/threads/workerpool.H>

class myJobObj : public x::jobObj {

// ...

};

typedef x::ref<myJobObj> myJob;

class servantObj;


x::workerpool<servantObj> workerpool= // ...


x::eventfd eventfdNotify(x::eventfd::create());

x::eventdestroynotify jobCompleted= ({

    myJob j(myJob::create());

    workerpool->run(j);

    x::eventdestroynotify::create(eventfdNotify, j);
});

// ...

while (!jobCompleted->wasdestroyed())
{
   eventfdNotify->event();
}

This example constructs a job object and submits it to a worker pool. x::eventdestroynotify::create() instantiates an event queue notification object. The main thread waits until the job completes by checking if the job object is destroyed, indicating that the thread worker finished the job, and since no other references to the job object are in scope, it gets destroyed. If the job is not done, the main execution thread waits for the event file descriptor.

x::eventdestroynotify's destructor callback sets an internal flag, and signals the event file descriptor (unless the event file descriptor went out of scope independently). wasdestroyed() retrieves the flag.