13.07.2015 Views

Intel® 64 and IA-32 Architectures Optimization Reference Manual

Intel® 64 and IA-32 Architectures Optimization Reference Manual

Intel® 64 and IA-32 Architectures Optimization Reference Manual

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

MULTICORE AND HYPER-THREADING TECHNOLOGYExample 8-3 shows the basic structure of a thread function that can be used in thisinterlaced producer-consumer model.Example 8-3. Thread Function for an Interlaced Producer Consumer Model// master thread starts first iteration, other thread must wait// one iterationvoid producer_consumer_thread(int master){int mode = 1 - master; // track which thread <strong>and</strong> its designated// buffer indexunsigned int iter_num = workamount >> 1;unsigned int i=0;iter_num += master & workamount & 1;if (master) // master thread starts the first iteration{produce(buffs[mode],count);Signal(sigp[1-mode1],1); // notify producer task in follower// thread that it can proceedconsume(buffs[mode],count);Signal(sigc[1-mode],1);i = 1;}for (; i < iter_num; i++){WaitForSignal(sigp[mode]);produce(buffs[mode],count); // notify the producer task in// other threadSignal(sigp[1-mode],1);}WaitForSignal(sigc[mode]);consume(buffs[mode],count);Signal(sigc[1-mode],1);}8-9

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!