Using Threads

The HP aC++ run-time environment supports multi-threaded applications.

The following HP aC++ libraries are thread-safe:

In order to pick the thread safe version of I/O routines (cout, cin, cerr, and clog) when you include iostream.h in your source files, you can add the -D_THREAD_SAFE compile time flag to your compilation line.

To guarantee that your I/O results from one thread are not intermingled with I/O results from other threads, you must protect your I/O statement with locks. (Note, if you use locks, you do not have to use the -D_THREAD_SAFE compile time flag.) For example:

// create a mutex and initialize it pthread_mutex_t the_mutex; #ifdef _PTHREADS_DRAFT4 // for user threads pthread_mutex_init(&the_mutex, pthread_mutexattr_default); #else // for kernel threads pthread_mutex_init(&the_mutex, (pthread_mutexattr_t *)NULL); #endif pthread_mutex_lock(&the_mutex); cout << "something" ... ; pthread_mutex_unlock(&_the_mutex);

Note that conditional compilation may be necessary to accomodate both the user threads and the kernel threads interfaces, as in the above example.

Required Command-line Options

To use the multi-thread safe capabilities of the Standard C++ Library and the Tools.h++ Library, you need to specify the following options at both compile and link time:

WARNING: If you do not specify these options, a run-time error will be generated or multi-thread behavior will be incorrect.


Using -D__HPACC_THREAD_SAFE_RB_TREE

The current standard C++ library (libstd) and RogueWave's tools.h++ library (librwtool) are not thread safe if the underlying implementation rb_tree class is involved. In other words, if the tree header file (which includes tree.cc) under /opt/aCC/include/ is used, these libraries are not thread safe. Most likely, it is indirectly referenced by including the standard C++ library container class map or set headers, or by including a RogueWave tools.h++ header like tvset.h, tpmset.h, tpmset.h, tvset.h, tvmset.h, tvmset.h, tpmap.h, tpmmap.h, tpmmap.h, tvmap.h, tvmmap.h.

Since changing the current rb_tree implementation to make it thread safe would break binary compatibility, the preprocessing macro, __HPACC_THREAD_SAFE_RB_TREE, must be defined. Whether or not this macro is defined when compiling a file that includes the tree header, its use must be consistent. For example, a new object file compiled with the macro defined should not be linked with older ones that were compiled without the macro defined. Library providers whose library is built with the the macro defined may need to notify their users to also compile their source with the macro defined when the tree header is included.


Exception Handling

It is illegal to throw out of a thread.

The following example illustrates that you cannot catch an object which has been thrown in a different thread. To do so will result in a runtime abort since HP aC++ finds no available catch handler and terminate is called.

#include <pthread.h>
void foo() {
   int i = 10;
   throw i;
}
int main() {
   pthread_t tid;
   try {
      ret=pthread_create(&tid, 0, (void*(*)(void*))foo, 0);
   }
   catch(int n) {}
}

For More Information

See documentation on using threads and writing multi-threaded applications.