Safe Concurrency for Methods and Libraries

It is recommended that you check the documentation for all the libraries you link with and use only thread-safe libraries in your Threading Building Blocks application. If you use libraries that are not thread-safe, you need to be very sure that they are not used improperly (without proper mutual exclusion) by multiple threads concurrently.

In particular, you should make sure you use the thread-safe runtime libraries in Windows (luckily, failure to do so will cause a compile-time error to help you remember this). Therefore, use the /MDd or /MD compiler option with the Intel or Microsoft compilers, for debug or release builds, respectively.

Warning

Windows runtime libraries come in thread-safe and thread-unsafe forms. Using thread-unsafe versions with Intel Threading Building Blocks could cause undefined results.

In general, you should write your code so it does not need locks. Threading Building Blocks offers algorithms that promote implicit synchronization as an alternative to explicit synchronization with locks. But because that is not always possible, you need some guidance on locks.

The thread-safety rules for non-thread-safe libraries reduce to the following general principle: do not invoke methods or functions concurrently on the same object. Two tasks, or threads, can invoke a method or function concurrently on different objects, but not on the same object. A short way to say this is:you need to provide your own locking, and the kind ...

Get Intel Threading Building Blocks now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.