Содержащиеся вНайти другие документыРесурсы поддержки | Загрузить это руководство в формате PDF (1549 КБ)
Chapter 7 Safe and Unsafe InterfacesThis chapter defines MT-safety levels for functions and libraries. This chapter discusses the following topics: Thread SafetyThread safety is the avoidance of data races. Data races occur when data are set to either correct or incorrect values, depending upon the order in which multiple threads access and modify the data. When no sharing is intended, give each thread a private copy of the data. When sharing is important, provide explicit synchronization to make certain that the program behaves in a deterministic manner. A procedure is thread safe when the procedure is logically correct when executed simultaneously by several threads. At a practical level, safety falls into the following recognized levels.
An unsafe procedure can be made thread safe and able to be serialized by surrounding the procedure with statements to lock and unlock a mutex. Example 7–1 shows three simplified implementations of fputs() , initially thread unsafe. Next is a serializable version of this routine with a single mutex protecting the procedure from concurrent execution problems. Actually, the single mutex is stronger synchronization than is usually necessary. When two threads are sending output to different files by using fputs(), one thread need not wait for the other thread. The threads need synchronization only when sharing an output file. The last version is MT-safe. This version uses one lock for each file, allowing two threads to print to different files at the same time. So, a routine is MT-safe when the routine is thread safe, and the routine's execution does not negatively affect performance. Example 7–1 Degrees of Thread Safety/* not thread-safe */
fputs(const char *s, FILE *stream) {
char *p;
for (p=s; *p; p++)
putc((int)*p, stream);
}
/* serializable */
fputs(const char *s, FILE *stream) {
static mutex_t mut;
char *p;
mutex_lock(&m);
for (p=s; *p; p++)
putc((int)*p, stream);
mutex_unlock(&m);
}
/* MT-Safe */
mutex_t m[NFILE];
fputs(const char *s, FILE *stream) {
char *p;
mutex_lock(&m[fileno(stream)]);
for (p=s; *p; p++)
putc((int)*p, stream);
mutex_unlock(&m[fileno(stream)]0;
}
MT Interface Safety LevelsThe man pages for functions and interfaces indicate how well the function or interface supports threads. The ATTRIBUTES section of each man page lists the MT-Level attribute, which is set to one of the safety level categories listed in Table 7–1. These categories are explained more fully in the attributes(5) man page. If a man page does not state explicitly that a function is MT-Safe, you must assume that the function is unsafe. Table 7–1 Interface Safety Levels
Some functions have purposely not been made safe for the following reasons.
Note – The only way to be certain that a function with a name not ending in “_r” is MT-Safe is to check the function's manual page. Use of a function identified as not MT-Safe must be protected by a synchronizing device or by restriction to the initial thread. Reentrant Functions for Unsafe InterfacesFor most functions with unsafe interfaces, an MT-Safe version of the routine exists. The name of the MT-Safe routine is the name of the Unsafe routine with “_r” appended. For example, the MT-Safe version of asctime() is asctime_r(). The Table 7–2 “_r” routines are supplied in the Solaris environment. Table 7–2 Reentrant Functions
Async-Signal-Safe Functions in Solaris ThreadsFunctions that can safely be called from signal handlers are Async-Signal-Safe. The IEEE Std 1003.1–2004 (POSIX) standard defines Async-Signal-Safe functions, which are listed in Table 5–2. In addition to these standard Async-Signal-Safe functions, the following functions from the Solaris threads interface are also Async-Signal-Safe: MT Safety Levels for LibrariesAll routines that can potentially be called by a thread from a multithreaded program should be MT-Safe. Therefore, two or more activations of a routine must be able to correctly execute concurrently. So, every library interface that a multithreaded program uses must be MT-Safe. Not all libraries are now MT-Safe. The commonly used libraries that are MT-Safe are listed in the following table. The libraries are accessed in the /usr/lib directory. Table 7–3 Some MT-Safe Libraries
Unsafe LibrariesRoutines in libraries that are not guaranteed to be MT-Safe can safely be called by multithreaded programs only when such calls are single threaded. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||