International standards are now making it practicable for developers to write portable multi-threaded applications. Consequently there is an increasing demand for Library developers to produce software that is thread safe.
In a Fortran 77 context the constructs that prohibit thread safety are, potentially, DATA, SAVE, COMMON and EQUIVALENCE. This is because such constructs define data that will be shared by different threads, perhaps leading to unwanted interactions between them; for example, the possibility that one thread may be modifying the contents of a COMMON block at the same time as another thread is reading it. You are therefore advised to avoid the use of such constructs wherever possible within multi-threaded applications.
At Mark 19 of the NAG Library the use of unsafe constructs has been eliminated from the majority of routines in the Library, making them thread safe. However, there are some routines where complete removal of these constructs would seriously affect their interface design and usability. In such cases it makes more sense to keep the routines unchanged and give clear warnings in the documentation that care should be taken when calling such routines in a multi-threaded context. It should be noted that it is safe to call any NAG routine in one thread (only) of a multi-threaded application.
Some Library routines require you to supply a routine and to pass the name of the routine as an argument in the call to the Library routine. It is often the case that you need to supply your routine with more information than can be given via the interface argument list. In such circumstances it is usual to define a COMMON block containing the required data in the supplied routine (and also in the calling program). It is safe to do this only if no data referenced in the defined COMMON block is updated within the supplied routine (thus avoiding the possibility of simultaneous modification by different threads). Where separate calls are made to a Library routine by different threads and these calls require different data sets to be passed through COMMON blocks to user-supplied routines, these routines and the COMMON blocks defined within them should have different names.
You are also advised to check whether the Library routines you intend to call have equivalent reverse communication interfaces, which are designed specifically for problems where user-supplied routine interfaces are not flexible enough for a given problem; their use should eliminate the need to provide data through COMMON blocks.
The Library contains routines for setting the current error and advisory message unit numbers (X04AAF and X04ABF). These routines use the SAVE statement to retain the values of the current unit numbers between calls. It is therefore not advisable for different threads of a multi-threaded program to set the message unit numbers to different values. A consequence of this is that error or advisory messages output simultaneously may become garbled, and in any event there is no indication of which thread produces which message. You are therefore advised always to select the `soft failure' mechanism without any error message (IFAIL = +1, see Section 2.3 of Essential Introduction) on entry to each NAG routine called from a multi-threaded application; it is then essential that the value of IFAIL is tested on return to the application.
A related problem is that of multiple threads writing to or reading from files. You are advised to make different threads use different unit numbers for opening files and to give these files different names (perhaps by appending an index number to the file basename). The only alternative to this is for you to protect each write to a file or unit number; for example, by putting each WRITE statement in a critical region.
You are also advised to refer to the Users' Note for details of whether the Library has been compiled in a manner that facilitates the use of multiple threads. Please note however that at Mark 19 the routines listed in the following table are not thread safe in any implementations.