JavaScript is required to for searching.
Skip Navigation Links
Exit Print View
Oracle Solaris Studio 12.3: C++ User's Guide     Oracle Solaris Studio 12.3 Information Library
search filter icon
search icon

Document Information

Preface

Part I C++ Compiler

1.  The C++ Compiler

2.  Using the C++ Compiler

3.  Using the C++ Compiler Options

Part II Writing C++ Programs

4.  Language Extensions

5.  Program Organization

6.  Creating and Using Templates

7.  Compiling Templates

8.  Exception Handling

9.  Improving Program Performance

10.  Building Multithreaded Programs

10.1 Building Multithreaded Programs

10.1.1 Indicating Multithreaded Compilation

10.1.2 Using C++ Support Libraries With Threads and Signals

10.2 Using Exceptions in a Multithreaded Program

10.2.1 Thread Cancellation

10.3 Sharing C++ Standard Library Objects Between Threads

10.4 Memory Barrier Intrinsics

Part III Libraries

11.  Using Libraries

12.  Using the C++ Standard Library

13.  Using the Classic iostream Library

14.  Building Libraries

Part IV Appendixes

A.  C++ Compiler Options

B.  Pragmas

Glossary

Index

10.4 Memory Barrier Intrinsics

The compiler provides the header file mbarrier.h, which defines various memory barrier intrinsics for SPARC and x86 processors. These intrinsics may be of use for developers writing multithreaded code using their own synchronization primitives. Refer to the appropriate processor documentation to determine when and if these intrinsics are necessary for their particular situation.

Memory ordering intrinsics supported by mbarrier.h include the following:

All the barrier intrinsics with the exception of the __compiler_barrier() intrinsic generate memory ordering instructions on x86, these are mfence, sfence, or lfence instructions. On SPARC platforms these are membar instructions.

The __compiler_barrier() intrinsic generates no instructions and instead informs the compiler that all previous memory operations must be completed before any future memory operations are initiated. The practical result is that all non-local variables and local variables with the static storage class specifier will be stored back to memory before the barrier, and reloaded after the barrier, and the compiler will not mix memory operations from before the barrier with those after the barrier. All other barriers implicitly include the behaviour of the __compiler_barrier() intrinsic.

For example, in the following code the presence of the __compiler_barrier() intrinsic stops the compiler from merging the two loops:

#include "mbarrier.h"
int thread_start[16];
void start_work()
{
/* Start all threads */
   for (int i=0; i<8; i++)
   {
     thread_start[i]=1;
   }
   __compiler_barrier();
/* Wait for all threads to complete */
   for (int i=0; i<8; i++)
   {
      while (thread_start[i]==1){}
   }
}