Threads and memory usage

If you are using threads for SMP performance then you need toconsider how you allocate and use variables and data between threads. Consider thisexample.


    Global1 : INTEGER;

    Global2 : INTEGER;

In this example thread #1 is only accessing Global1 and thread#1 is only accessing Global2. This is a bad situation because each thread will beflushing the other threads processor cache because both of the data items exist inthe same processor cache line. Performance here will be very bad. The size of a processorcache line varies significantly. The general rule of thumb is to have each threadaccessing separate memory. This obviously excludes shared data that is protectedwith some synchronization mechanism. Memory that is only read does not pose performanceissues, it is the written memory that needs to be handled properly for maximum performance.