Example 7-8 – Intel ARCHITECTURE IA-32 User Manual

Page 387

Advertising
background image

Multi-Core and Hyper-Threading Technology

7

7-41

Example 7-8 shows the batched implementation of the producer and
consumer thread functions.

Example 7-8

Batched Implementation of the Producer Consumer Threads

void producer_thread()

{

int iter_num = workamount - batchsize;
int mode1;

for (mode1=0; mode1 < batchsize; mode1++)

{

produce(buffs[mode1],count); }

while (iter_num--)
{

Signal(&signal1,1);

produce(buffs[mode1],count); // placeholder function
WaitForSignal(&end1);
mode1++;

if (mode1 > batchsize)

mode1 = 0;

}

}

void consumer_thread()
{

int mode2 = 0;
int iter_num = workamount - batchsize;

while (iter_num--)
{

WaitForSignal(&signal1);
consume(buffs[mode2],count); // placeholder function

Signal(&end1,1);
mode2++;
if (mode2 > batchsize)

mode2 = 0;

}
for (i=0;i<batchsize;i++)

{

consume(buffs[mode2],count);
mode2++;
if (mode2 > batchsize)

mode2 = 0;

}

}

Advertising