Multithreading Overview

Web Site

Research has shown that there is an inherent tradeoff between using "shared instance" (also called copy-on-write) and single-instance implementations of string classes.

Shared instance versions outperform the alternative by a wide margin for most real-world single-threaded and multi-threaded but single-CPU applications. However, they are not thread safe.

Introducing thread safety on SMP (symmetric multi-processor) architectures causes lock contention and a lot of CPU bus thrashing.  This is why developers coding for multiple-CPU boxes are often forced to revert to manual passing of strings between the two.

Str Library is an instance-shared implementation. It, however, takes a smart approach to building effective multithreaded systems even on SMP architectures: a semi-manual control of threading.

The following basic rules apply to using Str class instances in multithreaded applications:

To summarize, if you have a string object that may be modified by multiple threads, you need to initialize it as following:

Example 1:

	class MyClass { // 	May be accessed by various threads
		Str m_SomeMtData;
		MyClass() { m_SomeMtData.SetMT(); } 	// Mark as MT-enabled
	}

Example 2:

	class MyClass { // 	May be accessed by various threads
		Str m_SomeMtData (StrMT);
		MyClass() { }	// No need to call SetMT, the ctor has done it for us
	}

The symbol StrMT is a predefined constant that has no other special meaning except for allowing the compiler to distinguish between the regular and MT-enabling version of a constructor.

 

Performance tip: By default, Str Library will use its own free-block-management scheme. This improves performance singificantly on most systems and platforms, but may be detrimental to the speed of applications running on SMP machines.

We strongly recommend that, when targeting multi-CPU hardware, you turn off Str Library's free block caching mechanism by defining STR_NO_BLOCK_CACHE

Please see Free block management for more information.

See alsoStr conditional defines, SetMT