4.4 Thread Resources
Threads share most of its resources with other threads of the same process. Threads do own resources that define the thread's context. This includes the thread id, set of registers including the stack pointer and program counter, and stack. Threads must share other resources such as the processor, memory, and file descriptors required in order for it to perform its task. File descriptors are allocated to each process separately and threads of the same process compete for access to these descriptors. In memory, the processor, and other globally allocated resources, threads contend with other threads of its process as well as the threads of other processes for access to these resources.
A thread can allocate additional resources such as files or mutexes, but they are accessible to all the threads of the process. There are limits on the resources that can be consumed by a single process. Therefore, all the threads in combination must not exceed the resource limit of the process. If a thread attempts to consume more resources than the soft resource limit defines, it is sent a signal that the process's resource limit has been reached. Threads that allocate resources must be careful not to leave resources in an unstable state when they are canceled. A thread that has opened a file or created a mutex may be terminated, leaving the file open or the mutex locked. If the file has not been properly closed and the application is terminated, this may result in damage to the file or loss of data. A thread terminating after locking a mutex prevents access to whatever critical section that mutex is protecting. Before it terminates, a thread should perform some cleanup, preventing these unwanted situations from occurring.