Code:
struct MonitoredType
{
int (*event)(struct MonitoredType const* m); /*Return 0 to keep monitoring*/
struct AnyType data; /*Less, in size, than MIN_MONITOR_RANGE*/
char padding[MAX_MONITOR_RANGE - sizeof(AnyType)];
};
void wait_for_write(struct MonitoredType const* m)
{
/* This may miss a write if it happens before MONITOR, beware of race conditions if necessary */
do
{
_mm_monitor(&m->data, 0, 0);
_mm_mwait(0, 0);
} while ( ! m->event(m));
}
Any use of concurrency and parallelism can lead to buggy code if you don't use it the way it was meant to be used.