background preloader

Thread

Facebook Twitter

The Old New Thing : Interlocked operations don't solve ever. Interlocked operations are a high-performance way of updating DWORD-sized or pointer-sized values in an atomic manner. Note, however, that this doesn't mean that you can avoid the critical section. For example, suppose you have a critical section that protects a variable, and in some other part of the code, you want to update the variable atomically. "Well," you say, "this is a simple imcrement, so I can skip the critical section and just do a direct InterlockedIncrement.

Woo-hoo, I avoided the critical section bottleneck. " Well, except that the purpose of that critical section was to ensure that nobody changed the value of the variable while the protected section of code was running. You just ran in and changed the value behind that code's back. Conversely, some people suggested emulating complex interlocked operations by having a critical section whose job it was to protect the variable. // Wrong!

Oh no, our interlocked multiply isn't very interlocked after all! Dr. Dobb's | Writing Lock-Free Code: A Corrected Queue | Se. Dr. Dobb's | RapidMind: C++ Meets Multicore | június 8, 200. Azul Systems - Cliff Click Jr.’s Blog. TCMalloc : Thread-Caching Malloc. Motivation TCMalloc is faster than the glibc 2.3 malloc (available as a separate library called ptmalloc2) and other mallocs that I have tested. ptmalloc2 takes approximately 300 nanoseconds to execute a malloc/free pair on a 2.8 GHz P4 (for small objects). The TCMalloc implementation takes approximately 50 nanoseconds for the same operation pair. Speed is important for a malloc implementation because if malloc is not fast enough, application writers are inclined to write their own custom free lists on top of malloc.

This can lead to extra complexity, and more memory usage unless the application writer is very careful to appropriately size the free lists and scavenge idle objects out of the free list TCMalloc also reduces lock contention for multi-threaded programs. Another benefit of TCMalloc is space-efficient representation of small objects. Usage To use TCmalloc, just link tcmalloc into your application via the "-ltcmalloc" linker flag. $ LD_PRELOAD="/usr/lib/libtcmalloc.so" Overview.