"aminer" <
amin...@gmail.com> wrote in message
news:d73b1b3c-0705-49ee...@j10g2000vbe.googlegroups.com...
> Can you tell me if there any problem with my fifoqueue_mpmc ?
You should probably test it with Relacy:
http://www.1024cores.net/home/relacy-race-detector
AFAICT, you basically have a SPSC queue and that's fine. Anyway, you can
throw two locks around basically any single-producer/single-consumer queue
and get a MPMC out of it. FWIW, check out the following algorithm:
pseudo-code for a mpmc bounded queue of doubles:
______________________________________________
struct cell { uint32_t ver; double state; };
uint32_t head = 0;
uint32_t tail = 0;
cell cells[N]; // N must be a power of 2
void init() {
for (uint32_t i = 0; i < N; ++i) cells[i].ver = i;
}
void producer(double state) {
uint32_t ver = XADD(&head, 1);
cell& c = cells[ver & (N - 1)];
while (LOAD(&c.ver) != ver) backoff();
c.state = state;
STORE(&c.ver, ver + 1);
}
double consumer() {
uint32_t ver = XADD(&tail, 1);
cell& c = cells[ver & (N - 1)];
while (LOAD(&c.ver) != ver + 1) backoff();
double state = c.state;
STORE(&c.ver, ver + N);
return state;
}
______________________________________________
AFAICT, this is one of the more efficient algorithms out there...