You can optimize your code for a particular computer system by determining the cache characteristics of that system, and designing your code so that no two threads will ever need to write to the same cache block within performance-critical parallel loops. About the best you can hope to do without optimizing for a particular system would be to ensure that each thread has a private, page-aligned, segment of data. It is highly unlikely that any system would use a cache block as large as a page, because a page includes far too much varied data to provide any performance advantage in the memory management unit.

9 POSIX threads mini-reference

This chapter is a compact reference to the POSIX.1c standard.

9.1 POSIX1003.1c-1995 options

Pthreads is intended to address a wide variety of audiences. High-performance computational programs can use it to support parallel decomposition of loops. Realtime programs can use it to support concurrent realtime I/O. Database and network servers can use it to easily support concurrent clients. Business or software development programs can use it to take advantage of parallel and concurrent operations on time-sharing systems.

The Pthreads standard allows you to determine which optional capabilities are provided by the system, by defining a set of feature-test macros, which are shown in Table 9.1. Any implementation of Pthreads must inform you whether each option is supported, by three means:

• By making a formal statement of support in the POSIX Conformance Document. You can use this information to help design your application to work on specific systems.

• By defining compile-time symbolic constants in the <unistd.h> header file. You can test for these symbolic constants using #ifdef or #ifndef preprocessor conditionals to support a variety of Pthreads systems.

• By returning a positive nonzero value when the sysconf function is called with the associated sysconf symbol. (This is not usually useful for the 'feature-test' macros that specify whether options are present — if they are not, the associated interfaces usually are not supplied, and your code will not link, and may not even compile.)

You might, for example, choose to avoid relying on priority scheduling because after reading the conformance documents you discovered that three out of the four systems you wish to support do not provide the feature. Or you might prefer to use priority inheritance for your mutexes on systems that provide the feature, but write the code so that it will not try to access the mutex protocol attribute on systems that do not provide that option.

Symbolic constant, Description
sysconf symbol name
_POSIX_THREADS _SC_THREADS You can use threads (if your system doesn't define this, you're out of luck).
_POSIX_THREAD_ATTR_STACKSIZE _SC_THREAD_ATTR_STACKSIZE You can control the size of a thread's stack.
_POSIX_THREAD_ATTR_STACKADDR _SC_THREAD_ATTR_STACKADDR You can allocate and control a thread's stack.
_POSIX_THREAD_PRIORITY_SCHEDULING _SC_THREAD_PRIORITY_SCHEDULING You can use realtime scheduling.
_POSIX_THREAD_PRIO_INHERIT _SC_THREAD_PRIO_INHERIT You can create priority inheritance mutexes.
_POSIX_THREAD_PRIO_PROTECT _SC_THREAD_PRIO_PROTECT You can create priority ceiling mutexes.
_POSIX_THREAD_PROCESS_SHARED _SC_THREAD_PROCESS_SHARED You can create mutexes and condition variables that can be shared with another process.
_POSIX_THREAD_SAFE_FUNCTIONS _SC_THREAD_SAFE_FUNCTIONS You can use the special '_r' library functions that provide thread-safe behavior.

TABLE 9.1 POSIX 1003.1c-1995 options

9.2 POSIX1003.1c-1995 limits

The Pthreads standard allows you to determine the run-time limits of the system that may affect your application, for example, how many threads you can create, by defining a set of macros, which are shown in Table 9.2. Any implementation of Pthreads must inform you of its limits, by three means:

• By making a formal statement in the POSIX Conformance Document. You can use this information to help design your application to work on specific systems.

• By defining compile-time symbolic constants in the <limits.h> header file. The symbolic constant may be omitted from <limits.h> when the limit is at least as large as the required minimum, but cannot be determined at compile time, for example, if it depends on available memory space. You can test for these symbolic constants using #ifdef or #ifndef preprocessor conditionals.

• By returning a positive nonzero value when the sysconf function is called with the associated sysconf symbol.

You might, for example, design your application to rely on no more than 64 threads, if the conformance

Добавить отзыв
ВСЕ ОТЗЫВЫ О КНИГЕ В ИЗБРАННОЕ

0

Вы можете отметить интересные вам фрагменты текста, которые будут доступны по уникальной ссылке в адресной строке браузера.

Отметить Добавить цитату