Runtime Library Multi-Threading issues

Unless otherwise mentioned here a module in the runtime libraryis thread safe.


StartTime and GetTime use a global variable internally and thereforeare not thread safe. StartTimeEx and GetTimeEx are thread safe procedures.

Reading and writing files and pipes

None of the file I/O modules in the runtime library protect againstmultiple thread access. This allows for higher level protection mechanisms to beused when necessary. The reason for not protecting a single file access procedurecall is because this protection is generally not good enough. Consider the followingsituation

1. WriteBlock(FileA, ...);
3.WriteBlock(FileA, ...);

At position 2, another thread can perform a write even if theWriteBlock call protects itself against multiple access. What is really needed inthis situation is

  1. EnterProtectedState
  2. Perform a logical operation. (possibly multiple writes, reads, etc.)
  3. LeaveProtectedState

The Threads module provides multiple mechanisms for protectingaccess to a common object. For simple thread protection you can use CriticalSections,unnamed MutexSems, and spinlocks. For protection between multiple processes you canuse named MutexSem objects.

Protecting a File object can get in the way when two threadswant to write to different sections of a file. Generally you want to allow this formaximum performance.

The bottom line is that protecting access to files has much toconsider and the most efficient mechanisms are generally customized to the situationat hand and therefore the runtime library modules do not attempt to protect fileaccess to allow you to implement an ideal mechanism(s).

Pipes have the same considerations as files. Also remember thatpipes are only accessed on a first in, first out basis.

The FileFunc and TextFileFunc modules read and write to filesand pipes.

The NamedPipes module reads and writes pipes.


FileMap only supports one mapping per file, thefore multiplethreads cannot map multile file views. Multiple threads may use a mapping object,but access to this object is not protected and serialized just as normal file reads/writesare not protected. Using the mapped pointer in multiple threads has the same considerationsas reading and writing files.

ISO Channel I/O modules

StreamFile, SeqFile, RndFile, TermFile, ProgramArgs,
TextIO, WholeIO, RealIO, LongIO, RawIO, LWholeIO,
STextIO, SWholeIO, SRealIO, SLongIO, SRawIO, SIOResult, SLWholeIO,
IOChan, IOResult,

These modules read/write files and pipes and have the same considerationsas discussed for files and pipes.


This module can change the standard channel which is used bythe simple I/O modules. All threads in a process share the same standard channels.If you need to change the standard channel you should provide some protection forthis operation. You should make sure no threads can use the channel while you arechanging the channel.

Simple ISO I/O modules are STextIO, SWholeIO, SRealIO, SLongIO,SRawIO, SIOResult and SLWholeIO.


This module maintains various global variables that control thedate and time formatting. These globals are initialized with values fetched fromthe operating system date time format settings. If multiple threads do not need toalter these globals then multiple threads may use this module in a safe manner.

Threads, MemShare

These modules are thread safe except in the case of creationand destruction of the objects in the module. For maximum performance the modulesdo not protect creation and destruction of objects while an object is in use. Thismeans that one thread may be using the object, and another can destroy the objectby closing it.

MemShare is designed to control shared memory between multipleprocesses which by definition involves at least two threads, one per process, howeverthe shared memory is not destroyed until all processes have closed thier access andeach process maintains it own data structures referencing the shared memory.

MD5, SHA1, SHA256, SHA384, SHA512

Only one thread at a time may use a hash object.

AreSee4, RSA

Only one thread at a time may use an encryption object.

DES, AES, Blowfish

Only one thread at a time may use an encryption object, exceptfor the ECB mode.


This module maintains a global state of configuration settings. All threads share this global state. Writes to the settings are not protected against multiple access and therefore have the same considerations as discussed for files and pipes.


This module maintains a global state of a current registry directory.All threads share this global state. Writes to the registry are not protected againstmultiple access and therefore have the same considerations as discussed for filesand pipes.