Thread, Mutex and Semaphore
February 25th, 2008 by admin
What is a thread?A UNIX process contains both the executing program and a bundle of resources such as file descriptor table and address space. In MACH a task contains a bundle of resources; threads handle all execution activities. All threads associated with a given task share the task’s resources.Thus thread is essentially a program counter, a stack, and a set of registers - all the data structures belong to a task. Since threads are every small compared to processes, thread creation relatively cheap in terms of CPU costs. As processes require their own resources bundle, and thread share resources, threads are likewise memory frugal.A Thread is a path of execution through a program. It is also known as a lightweight process (sequential flow of control) which shares an address space and some other resources with other, and for which context switching time is lower than for heavyweight processes( kernel supported).Context switching between two threads is considerably cheaper than a context switch between two processes.In addition, the fact that all data except for stack and registers are shared between threads makes them a natural vehicle for expressing tasks that can be broken down into subtasks that can be run cooperatively.
What are Mutexes? How are they different from Semaphore?
The simplest and most commonly used synchronization object is the mutex . The phrase is derived from mutual exclusion. They are typically used to allow only one thread at a time access to a particular data structure, module or section of code. MFC use the CMutex class.
Semaphores are identical to mutexes except they maintain a count and are typically used to allow a limited number of threads access rather than enforcing mutual exclusion. Locking or acquiring semaphore decrements the count and releasing increments the count. When the count is zero the waiting thread is blocked until some other thread releases the semaphore and increments the count.
This entry was posted on Monday, February 25th, 2008 at 11:40 am and is filed under Technical. You can follow any responses to this entry through the RSS 2.0 feed. Responses are currently closed, but you can trackback from your own site.