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

Page 357

Advertising
background image

Multi-Core and Hyper-Threading Technology

7

7-11

It is possible to structure the producer-consumer model in an interlaced
manner such that it can minimize bus traffic and be effective on
multi-core processors without shared second-level cache.

In this interlaced variation of the producer-consumer model, each
scheduling quanta of an application thread comprises of a producer task
and a consumer task. Two identical threads are created to execute in
parallel. During each scheduling quanta of a thread, the producer task
starts first and the consumer task follows after the completion of the
producer task; both tasks work on the same buffer. As each task
completes, one thread signals to the other thread notifying its

Example 7-2

Basic Structure of Implementing Producer Consumer Threads

(a) Basic structure of a producer thread function
void producer_thread()
{

int iter_num = workamount - 1; // make local copy
int mode1 = 1; // track usage of two buffers via 0 and 1
produce(buffs[0],count); // placeholder function
while (iter_num--) {

Signal(&signal1,1); // tell the other thread to commence
produce(buffs[mode1],count); // placeholder function
WaitForSignal(&end1);
mode1 = 1 - mode1; // switch to the other buffer

}

}
b) Basic structure of a consumer thread
void consumer_thread()
{

int mode2 = 0; // first iteration start with buffer 0, than alternate
int iter_num = workamount - 1;
while (iter_num--) {

WaitForSignal(&signal1);
consume(buffs[mode2],count); // placeholder function
Signal(&end1,1);
mode2 = 1 - mode2;

}
consume(buffs[mode2],count);

}

Advertising