Example 7-3 Thread Function For An Interlaced Producer Consumer Model - Intel ARCHITECTURE IA-32 Reference Manual

Architecture optimization
Table of Contents

Advertisement

Example 7-3
Thread Function for an Interlaced Producer Consumer Model
// master thread starts the first iteration, the other thread must wait
// one iteration
void producer_consumer_thread(int master)
{
int mode = 1 - master; // track which thread and its designated buffer index
unsigned int iter_num = workamount >> 1;
unsigned int i=0;
iter_num += master & workamount & 1;
if (master) // master thread starts the first iteration
{
consume(buffs[mode],count);
i = 1;
}
for (; i < iter_num; i++)
{
WaitForSignal(sigp[mode]);
WaitForSignal(sigc[mode]);
consume(buffs[mode],count);
}
}
produce(buffs[mode],count);
Signal(sigp[1-mode],1); // notify the producer task in follower thread
// that it can proceed
Signal(sigc[1-mode],1);
produce(buffs[mode],count); // notify the producer task in other thread
Signal(sigp[1-mode],1);
Signal(sigc[1-mode],1);
Multi-Core and Hyper-Threading Technology
7
7-13

Advertisement

Table of Contents
loading

Table of Contents