A software thread is the “Smallest sequence of programmed instructions that can be managed independently by a scheduler” [Wikipedia]
A thread has its own:
- Program counter
- Registers
- Stack
- Thread-local memory (better to avoid in general)
- Threads of a process share everything else, e.g. Program code, constants, Network connections
To make code thread-safe, it is important to know what objects are shared and know when they are shared. The difficulty depends on the context.
Guidelines for writing thread-safe modules (from this wiki page).
- Remove class data members that are intended to be valid for each event.
- If
TFileService
is used in event-level functions, then- If using a shared module,
serialize(...)
must be called inside the constructor body, or - Use a legacy module
- If using a shared module,
For more information, please see:
- the wiki page Migration path for LArSoft services in art 3
- Making Code Thread-Safe by Kyle Knoepfel and
- Introduction to multi-threading and vectorization by Matti Kortelainen