[Source Code]


#include <iostream>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/thread.hpp>

void run(char* p, int size, int count)
{
  boost::posix_time::ptime begTick(boost::posix_time::microsec_clock::local_time());

  while (count--)
  {
    for (int i = 0; i < size; i++)
      p[i]++; // dummy operation
  }

  boost::posix_time::ptime endTick(boost::posix_time::microsec_clock::local_time());
  std::cout << boost::posix_time::to_simple_string(endTick - begTick) << std::endl;
}

int main()
{
  static const int SIZE  = 16;
  static const int COUNT = 100000000;
  __declspec(align(64)) char buf[1024]; // enough buffer size
  
  {
    char* p1 = buf;
    char* p2 = buf + 32;
    boost::thread thread1(run, p1, SIZE, COUNT);
    boost::thread thread2(run, p2, SIZE, COUNT);
    thread1.join();
    thread2.join();
  }

  {
    char* p1 = buf;
    char* p2 = buf + 64;
    boost::thread thread1(run, p1, SIZE, COUNT);
    boost::thread thread2(run, p2, SIZE, COUNT);
    thread1.join();
    thread2.join();
  }
}




[Output]


00:00:24.488401

00:00:24.496401

00:00:10.109578

00:00:10.141580




[Conclusion]


For the fast access of the memory in other threads, "false sharing" can harm the performance of its execution. You'd better know what the "false sharing" mechanism is.

For more information :  http://en.wikipedia.org/wiki/False_sharing




[Compile]


Microsoft Visual Studio 2008 - Release ( Optimization : Disabled (/Od) )