©2001 CRC Press LLC
system to determine if data protected by the mutex is in a transient state and would
cause problems if another thread were given control of the data.
A semaphore is similar to a mutex, but ownership is permitted by a specified
number of threads. An analogy of a semaphore is a crowded nightclub. If capacity
of the club is limited to 500 people, and 600 people want to enter the club, 100 are
forced to wait outside. The doorman is the semaphore, and restricts access to the
first 500 people. When a person exits, another individual from outside is allowed to
enter. A semaphore works the same way.
Mutexes and semaphores must be used in DLLs and code libraries if they are
to be considered thread-safe. LabVIEW can be configured to call DLLs from the
user interface subsystem, its primary thread, if it is unclear that the DLL is thread
safe. A programmer should never assume that code is thread safe; this can lead to
very difficult issues to resolve.
9.2 THREAD MECHANICS
All activities threads perform are documented in an operating system’s specification.
The actual behavior of the threads is dependent on a vendor’s implementation. In
the case of Windows, there is only one vendor, Microsoft. On the other hand, UNIX
has a number of vendors who implement the standard in slightly different ways.
Providing detailed information on operating system-specific details for all the UNIX
flavors is beyond the scope of this book.
Regardless of operating system, all threads have a few things in common. First,
threads must be given time on the CPU to execute. The operating system scheduler
determines which threads get time on the CPU. Second, all threads have a concept
of state; the state of a thread determines its eligibility to be given time on the CPU.
9.2.1 T
HREAD
S
TATES
A thread can be in one of three states: Active, Blocked, or Suspended. Active threads
will be arranged according to their priority and allocated time on the CPU. An active
thread may become blocked during its execution. In the event an executing thread
becomes blocked, it will be moved into the inactive queue, which will be explained
shortly.
Threads that are blocked are currently waiting on a resource from the operating
system (a Kernel object or message). For example, when a thread tries to access the
hard drive of the system, there will be a delay on the order of 10 ms for the hard
drive to respond. The operating system blocks this thread because it is now waiting
for a resource and would otherwise waste time on the CPU. When the hard drive
triggers an interrupt, it informs the operating system it is ready. The operating system
will signal the blocked thread and the thread will be put back into a run queue.
Suspended threads are “sleeping.” For example, in C, using the Sleep statement
will suspend the thread that executed the command. The operating system effectively
treats blocked and suspended threads in the same fashion; they are not allowed time
on the CPU. Both suspended and blocked threads are allowed to resume execution
when they have the ability to, when they are signaled.